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

# Retrieve Users

> Fetch CometChat users in Flutter apps, get logged-in user details, and filter user lists with request builder options.

## Retrieve Logged In User Details

You can get the details of the logged-in user using the `getLoggedInUser()` method. This method can also be used to check if the user is logged in or not. If the method returns `null`, it indicates that the user is not logged in and you need to log the user into CometChat.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    User user = await CometChat.getLoggedInUser()   
    ```
  </Tab>
</Tabs>

This method will return a `User` object containing all the information related to the logged-in user.

## Retrieve List of Users

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

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

### Set Limit

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

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    UsersRequest usersRequest = (UsersRequestBuilder()
      ..limit = 50
      ).build();
    ```
  </Tab>
</Tabs>

### Set Search Keyword

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

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

### Set Status

The status based on which the users are to be fetched. The status parameter can contain one of the below two values:

* CometChatUserStatus.online - will return the list of only online users.
* CometChatUserStatus.offline - will return the list of only offline users.

If this parameter is not set, will return all the available users.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    UsersRequest usersRequest = (UsersRequestBuilder()
      ..limit = 50
    	..userStatus = CometChatUserStatus.online
      ).build(); 
    ```
  </Tab>
</Tabs>

If this parameter is not set, will return all users.

### Hide Blocked Users

This method is used to determine if the blocked users should be returned as a part of the user list. If set to `true`, the user list will not contain the users blocked by the logged-in user.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    UsersRequest usersRequest = (UsersRequestBuilder()
      ..limit = 50
    	..hideBlockedUsers = true
      ).build();  
    ```
  </Tab>
</Tabs>

### Set Roles

This method allows you to fetch the users based on multiple roles.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    List<String> roles = [];
    roles.add("role1");
    roles.add("role2");
    UsersRequest usersRequest = (UsersRequestBuilder()
      ..limit = 50
    	..roles = roles
      ).build();
    ```
  </Tab>
</Tabs>

### Friends Only

This property when set to true will return only the friends of the logged-in user.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    UsersRequest usersRequest = (UsersRequestBuilder()
      ..limit = 50
    	..friendsOnly = true
      ).build();
    ```
  </Tab>
</Tabs>

### Set Tags

This method accepts a list of tags based on which the list of users is to be fetched. The list fetched will only contain the users that have been tagged with the specified tags.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    List<String> tags = [];
    tags.add("tag1");
    tags.add("tag2");
    UsersRequest usersRequest = (UsersRequestBuilder()
      ..limit = 50
    	..tags = tags
      ).build();  
    ```
  </Tab>
</Tabs>

### With Tags

This property when set to true will fetch tags data along with the list of users.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    List<String> tags = [];
    tags.add("tag1");
    tags.add("tag2");
    UsersRequest usersRequest = (UsersRequestBuilder()
      ..limit = 50
    	..withTags = true
      ).build();
    ```
  </Tab>
</Tabs>

### Set UIDs

This method accepts a list of UIDs based on which the list of users is fetched. A maximum of 25 users can be fetched.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    List<String> uids = [];
    uids.add("UID1");
    uids.add("UID2");
    UsersRequest usersRequest = (UsersRequestBuilder()
      ..limit = 25
    	..uids = uids
      ).build(); 
    ```
  </Tab>
</Tabs>

### Search In

This method allows you to specify which user fields to search in when using the search keyword. You can search in `name`, `uid`, or both.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    UsersRequest usersRequest = (UsersRequestBuilder()
      ..limit = 50
      ..searchKeyword = "john"
      ..searchIn = ["name", "uid"]
      ).build();
    ```
  </Tab>
</Tabs>

### Sort By

This method allows you to specify the field by which the users should be sorted. You can sort by `name`, `status`, or `createdAt`.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    UsersRequest usersRequest = (UsersRequestBuilder()
      ..limit = 50
      ..sortBy = "name"
      ).build();
    ```
  </Tab>
</Tabs>

### Sort By Order

This method allows you to specify the order in which the users should be sorted. You can use `asc` for ascending or `desc` for descending order.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    UsersRequest usersRequest = (UsersRequestBuilder()
      ..limit = 50
      ..sortBy = "name"
      ..sortByOrder = "asc"
      ).build();
    ```
  </Tab>
</Tabs>

You can combine these parameters for more precise user filtering and ordering:

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    UsersRequest usersRequest = (UsersRequestBuilder()
      ..limit = 50
      ..searchKeyword = "john"
      ..searchIn = ["name"]
      ..sortBy = "name"
      ..sortByOrder = "asc"
      ).build();
    ```
  </Tab>
</Tabs>

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

Once you have the object of the `UsersRequest` class, you need to call the `fetchNext()` method. Calling this method will return a list of `User` objects containing 'n' number of users depending on the limit set.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    UsersRequest usersRequest = (UsersRequestBuilder()
      ..limit = 25
      ).build();

    usersRequest.fetchNext(onSuccess: (List<User> userList){
        debugPrint("User List Fetched Successfully : $userList");
      },onError: (CometChatException e){
        debugPrint("User List Fetch Failed: ${e.message}");
      });
    ```
  </Tab>
</Tabs>

## Retrieve Particular User Details

To get the information of a user, you can use the `getUser()` method.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    String UID = "UID";
      
    CometChat.getUser(UID, onSuccess: (User user){
        debugPrint("User Fetched Successfully : $user");
      }, onError: (CometChatException e){
        debugPrint("User Fetch Failed: ${e.message}");
      });
    ```
  </Tab>
</Tabs>

The `getUser()` method takes the following parameters:

| Parameter | Description                                                |
| --------- | ---------------------------------------------------------- |
| `UID`     | The UID of the user for whom the details are to be fetched |

On success, the `User` object containing the details of the user is returned.

## Get online user count

To get the total count of online users for your app, you can use the `getOnlineUserCount()` method.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    CometChat.getOnlineUserCount(onSuccess: (int count){
        debugPrint("Online User Count: $count"); 
      }, onError: (CometChatException e){
        debugPrint("User Count Fetch Failed: ${e.message}");
      });
    ```
  </Tab>
</Tabs>
