Skip to main content
Enable users to block and unblock other users in your Flutter chat app using CometChat’s blockUsers and unblockUsers methods.

Overview

The Block User feature lets one user prevent another from sending messages or interacting with them. Essential for user privacy, spam control, and moderation.

Prerequisites

  • A Flutter project with CometChat UIKit Flutter V6 installed
  • Initialized CometChat credentials (appID, region, authKey)

Components

ComponentRole
CometChatUIKit.blockUsers([...])SDK method to block specified user(s)
CometChatUIKit.unblockUsers([...])SDK method to unblock specified user(s)
ElevatedButtonFlutter widget for block/unblock actions

Integration Steps

Add “Block User” Button

ElevatedButton(
  onPressed: () async {
    try {
      await CometChatUIKit.blockUsers([user.uid]);
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('${user.name} has been blocked')),
      );
    } catch (e) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('Error blocking user: $e')),
      );
    }
  },
  child: Text('Block User'),
)

Add “Unblock User” Button

ElevatedButton(
  onPressed: () async {
    try {
      await CometChatUIKit.unblockUsers([user.uid]);
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('${user.name} has been unblocked')),
      );
    } catch (e) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('Error unblocking user: $e')),
      );
    }
  },
  child: Text('Unblock User'),
)

Customization Options

  • Conditional Rendering: Display “Block” or “Unblock” based on user.blockedByMe state
  • Modal Confirmation: Wrap actions in showDialog for confirmation prompts
  • Self-Block Prevention: Disable the button if user.uid == loggedInUser.uid

Summary

FeatureMethod
Block UserCometChatUIKit.blockUsers([...])
Unblock UserCometChatUIKit.unblockUsers([...])