> ## Documentation Index
> Fetch the complete documentation index at: https://cometchat-22654f5b-docs-llms-scoped-indexes.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Block Users

> Learn how to block and unblock users in your Flutter app using the CometChat SDK to manage user interactions and privacy.

<Accordion title="AI Integration Quick Reference">
  ```dart theme={null}
  // Block users
  List<String> uids = ["UID1", "UID2"];
  CometChat.blockUser(uids, onSuccess: (Map<String, dynamic> map) {
    debugPrint("Blocked: $map");
  }, onError: (CometChatException e) {
    debugPrint("Error: ${e.message}");
  });

  // Unblock users
  CometChat.unblockUser(uids, onSuccess: (Map<String, dynamic> map) {
    debugPrint("Unblocked: $map");
  }, onError: (CometChatException e) {
    debugPrint("Error: ${e.message}");
  });

  // Get blocked users list
  BlockedUsersRequest request = (BlockedUsersRequestBuilder()..limit = 30).build();
  request.fetchNext(onSuccess: (List<User> users) { }, onError: (e) { });
  ```

  **Directions:** `directionBlockedByMe` | `directionHasBlockedMe` | `directionBoth` (default)
</Accordion>

Block users to prevent them from sending messages to the logged-in user. This feature helps users manage their privacy and control who can communicate with them.

<Note>
  **Available via:** SDK | [REST API](https://api-explorer.cometchat.com) | [UI Kits](/ui-kit/flutter/overview)
</Note>

## Block Users

*In other words, as a logged-in user, how do I block a user from sending me messages?*

You can block users using the `blockUser()` method. Once any user is blocked, all the communication to and from the respective user will be completely blocked. You can block multiple users in a single operation. The `blockUser()` method takes a `List<String>` as a parameter which holds the list of `UIDs` to be blocked.

### Parameters

| Parameter | Type | Description |
| - | - | - |
| `uids` | `List<String>?` | List of UIDs of users to block |
| `onSuccess` | `Function(Map<String, dynamic> map)?` | Callback triggered on successful block operation |
| `onError` | `Function(CometChatException excep)?` | Callback triggered on error |

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    List<String > uids = [];
    uids.add("UID1");
    uids.add("UID2");
    uids.add("UID3");
    CometChat.blockUser(uids, onSuccess: (Map<String, dynamic> map) {
    debugPrint("Blocked User Successfully $map ");
    }, onError: (CometChatException e) {
    debugPrint("Blocked User Unsuccessful ${e.message} ");
    });
    ```
  </Tab>
</Tabs>

<Accordion title="Response">
  **On Success** — A `Map<String, dynamic>` containing UIDs as keys and `"success"` or `"fail"` as values indicating the result of each block operation:

  <span id="block-users-map-object" style={{scrollMarginTop: '100px'}} />

  **Map Object:**

  | Parameter | Type | Description | Sample Value |
  | - | - | - | - |
  | `UID1` | string | Block result for UID1 | `"success"` |
  | `UID2` | string | Block result for UID2 | `"success"` |
  | `UID3` | string | Block result for UID3 | `"success"` |
</Accordion>

<Accordion title="Error">
  | Parameter | Type | Description | Sample Value |
  | - | - | - | - |
  | `code` | string | Error code identifier | `"ERR_UID_NOT_FOUND"` |
  | `message` | string | Human-readable error message | `"The specified UID does not exist."` |
  | `details` | string | Additional technical details | `"Please verify the UID and try again."` |
</Accordion>

In the `onSuccess()` callback, you receive a Map which contains `UIDs` as the keys and `success` or `fail` as the value based on if the block operation for the `UID` was successful or not.

## Unblock Users

*In other words, as a logged-in user, how do I unblock a user I previously blocked?*

You can unblock the already blocked users using the `unblockUser()` method. You can unblock multiple users in a single operation. The `unblockUser()` method takes a `List<String>` as a parameter which holds the list of `UIDs` to be unblocked.

### Parameters

| Parameter | Type | Description |
| - | - | - |
| `uids` | `List<String>?` | List of UIDs of users to unblock |
| `onSuccess` | `Function(Map<String, dynamic> map)?` | Callback triggered on successful unblock operation |
| `onError` | `Function(CometChatException excep)?` | Callback triggered on error |

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    List<String > uids = [];
    uids.add("UID1");
    uids.add("UID2");
    uids.add("UID3");

    CometChat.unblockUser(uids, onSuccess: (Map<String, dynamic> map) {
     debugPrint("Blocked User Successfully $map ");
     }, onError: (CometChatException e) {
     debugPrint("Blocked User Unsuccessful ${e.message} ");
     });
    ```
  </Tab>
</Tabs>

<Accordion title="Response">
  **On Success** — A `Map<String, dynamic>` containing UIDs as keys and `"success"` or `"fail"` as values indicating the result of each unblock operation:

  <span id="unblock-users-map-object" style={{scrollMarginTop: '100px'}} />

  **Map Object:**

  | Parameter | Type | Description | Sample Value |
  | - | - | - | - |
  | `UID1` | string | Unblock result for UID1 | `"success"` |
  | `UID2` | string | Unblock result for UID2 | `"success"` |
  | `UID3` | string | Unblock result for UID3 | `"success"` |
</Accordion>

<Accordion title="Error">
  | Parameter | Type | Description | Sample Value |
  | - | - | - | - |
  | `code` | string | Error code identifier | `"ERR_UID_NOT_FOUND"` |
  | `message` | string | Human-readable error message | `"The specified UID does not exist."` |
  | `details` | string | Additional technical details | `"Please verify the UID and try again."` |
</Accordion>

In the `onSuccess()` callback, you receive a Map which contains `UIDs` as the keys and `success` or `fail` as the value based on if the unblock operation for the `UID` was successful or not.

## Get list of blocked users

*In other words, as a logged-in user, how do I get a list of all users I've blocked?*

In order to fetch the list of blocked users, you can use the `BlockedUsersRequest` class. To use this class i.e to create an object of the `BlockedUsersRequest class`, you need to use the `BlockedUsersRequestBuilder` class. The `BlockedUsersRequestBuilder` class allows you to set the parameters based on which the blocked users are to be fetched.

The `BlockedUsersRequestBuilder` class allows you to set the below parameters:

### BlockedUsersRequestBuilder

| Parameter | Type | Description |
| - | - | - |
| `limit` | `int?` | Number of blocked users to fetch per request. Max: 100, Default: 30 |
| `searchKeyword` | `String?` | Keyword to filter blocked users by name |
| `direction` | `String?` | Direction of block — `directionBlockedByMe`, `directionHasBlockedMe`, or `directionBoth` (default) |
| `setPage` | `int?` | Fetch blocked users from a particular page |

### Set Limit

This method sets the limit i.e. the number of blocked users that should be fetched in a single iteration.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    BlockedUsersRequest blockedUsersRequest =   (BlockedUsersRequestBuilder()
      ..limit = 50
      ).build();
    ```
  </Tab>
</Tabs>

### Set Search Keyword

This method allows you to set the search string based on which the blocked users are to be fetched.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    BlockedUsersRequest blockedUsersRequest =  (BlockedUsersRequestBuilder()
      ..limit = 50
      ..searchKeyword = "abc"
      ).build();
    ```
  </Tab>
</Tabs>

### Set Direction

* `CometChatBlockedUsersDirection.`*directionBlockedByMe* - This will ensure that the list of blocked users only contains the users blocked by the logged in user.
* `CometChatBlockedUsersDirection.`*directionHasBlockedMe* - This will ensure that the list of blocked users only contains the users that have blocked the logged in user.
* `CometChatBlockedUsersDirection.`*directionBoth* - This will make sure the list of users includes both the above cases. This is the default value for the direction variable if it is not set.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    BlockedUsersRequest blockedUsersRequest = (BlockedUsersRequestBuilder()
      ..limit = 50
      ..direction = CometChatBlockedUsersDirection.directionBlockedByMe
      ).build();  
    ```
  </Tab>
</Tabs>

Finally, once all the parameters are set in the builder class, you need to call the `build()` method to get the object of the `BlockedUsersRequest` class.

Once you have the object of the `BlockedUsersRequest` class, you need to call the `fetchNext()` method. Calling this method will return a list of `User` objects containing n number of blocked users where N is the limit set in the builder class.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    BlockedUsersRequest blockedUsersRequest = (BlockedUsersRequestBuilder()
        ..limit = 30
      ).build();

    blockedUsersRequest.fetchNext(onSuccess: (List<User> userList){
        debugPrint("Custom Message Sent Successfully : $userList ");
      }, onError: (CometChatException e){
        debugPrint("Blocked User Request failed with exception: ${e.message}");
    }); 
    ```
  </Tab>
</Tabs>

<Accordion title="Response">
  **On Success** — A `List<User>` containing the blocked users:

  <span id="fetch-blocked-users-user-object" style={{scrollMarginTop: '100px'}} />

  **User Object (per item in list):**

  | Parameter | Type | Description | Sample Value |
  | - | - | - | - |
  | `uid` | string | Unique identifier of the user | `"cometchat-uid-3"` |
  | `name` | string | Display name of the user | `"Kevin Hart"` |
  | `link` | string | Profile link | `null` |
  | `avatar` | string | Avatar URL | `"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-3.webp"` |
  | `metadata` | object | Custom metadata | `{}` |
  | `status` | string | Online status | `"offline"` |
  | `role` | string | User role | `"default"` |
  | `statusMessage` | string | Status message | `null` |
  | `tags` | array | User tags | `[]` |
  | `hasBlockedMe` | boolean | Whether this user has blocked the current user | `false` |
  | `blockedByMe` | boolean | Whether the current user has blocked this user | `true` |
  | `lastActiveAt` | number | Epoch timestamp of last activity | `1745550000` |
</Accordion>

<Accordion title="Error">
  | Parameter | Type | Description | Sample Value |
  | - | - | - | - |
  | `code` | string | Error code identifier | `"ERR_UID_NOT_FOUND"` |
  | `message` | string | Human-readable error message | `"The specified UID does not exist."` |
  | `details` | string | Additional technical details | `"Please verify the UID and try again."` |
</Accordion>

Relevant fields to access on returned [`User`](/sdk/reference/entities#user) objects:

| Field | Type | Description |
| - | - | - |
| `blockedByMe` | `bool` | Whether the logged-in user has blocked this user |
| `hasBlockedMe` | `bool` | Whether this user has blocked the logged-in user |

***

## Next Steps

<CardGroup cols={2}>
  <Card title="Retrieve Users" icon="users" href="/sdk/flutter/retrieve-users">
    Fetch and filter users from your CometChat app
  </Card>

  <Card title="User Management" icon="user-gear" href="/sdk/flutter/user-management">
    Create, update, and delete users programmatically
  </Card>

  <Card title="User Presence" icon="circle-dot" href="/sdk/flutter/user-presence">
    Track online/offline status of users in real-time
  </Card>

  <Card title="Users Overview" icon="address-book" href="/sdk/flutter/users-overview">
    Complete guide to user features in CometChat
  </Card>
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.