Skip to main content

Documentation Index

Fetch the complete documentation index at: https://www.cometchat.com/docs/llms.txt

Use this file to discover all available pages before exploring further.

// Fetch users list
UsersRequest usersRequest = (UsersRequestBuilder()..limit = 30).build();
usersRequest.fetchNext(
  onSuccess: (List<User> userList) => debugPrint("Users: $userList"),
  onError: (CometChatException e) => debugPrint("Error: ${e.message}")
);

// Get specific user details
CometChat.getUser("UID",
  onSuccess: (User user) => debugPrint("User: $user"),
  onError: (CometChatException e) => debugPrint("Error: ${e.message}")
);

// Get logged-in user
User? user = await CometChat.getLoggedInUser();

// Get online user count
CometChat.getOnlineUserCount(
  onSuccess: (int count) => debugPrint("Online: $count"),
  onError: (CometChatException e) => debugPrint("Error: ${e.message}")
);
The CometChat SDK provides methods to retrieve the logged-in user, fetch filtered user lists, look up individual users by UID, and get online user counts. All user methods return User objects.
Available via: SDK | REST API | UI Kits

User Object Fields

FieldTypeDescription
uidStringUnique user ID
nameStringDisplay name of the user
avatarString?URL of the user’s avatar image
statusStringOnline status of the user ("online" or "offline")
lastActiveAtint?Epoch timestamp when the user was last active
roleStringRole assigned to the user
tagsList<String>Tags associated with the user

Retrieve Logged In User Details

Use getLoggedInUser() to get the current user’s details. Returns null if no user is logged in.
User? user = await CometChat.getLoggedInUser();
This method returns a User object with the logged-in user’s information.

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. Fetching using this builder will return User objects.

UsersRequestBuilder Parameters

ParameterTypeDescription
limitint?Number of users to fetch per request
searchKeywordString?Filters users by a search string
searchInList<String>?Specifies which user properties to search ("uid", "name"). Works with searchKeyword.
userStatusString?Filters by online status (CometChatUserStatus.online or CometChatUserStatus.offline)
hideBlockedUsersbool?When true, excludes users blocked by the logged-in user
rolesList<String>?Filters users by specified roles
friendsOnlybool?When true, returns only friends of the logged-in user
tagsList<String>?Filters users by specified tags
withTagsbool?When true, includes tag data in the returned user objects
uidsList<String>?Fetches specific users by their UIDs. Maximum 25 per request.
sortByString?Sorts the user list by a specific property. Default sort order: status → name → UID. Pass "name" to sort by name → UID.
sortByOrderString?Sets the sort order. Default is ascending ("asc"). Use "desc" for descending.
The UsersRequestBuilder class allows you to set the below parameters:

Set Limit

Sets the number of users to fetch per request.
UsersRequest usersRequest = (UsersRequestBuilder()
  ..limit = 50
  ).build();

Set Search Keyword

Filters users by a search string.
UsersRequest usersRequest = (UsersRequestBuilder()
  ..limit = 50
	..searchKeyword = "abc"
  ).build();

Search In

Specifies which user properties to search. Works with searchKeyword. By default, searches both UID and name.
UsersRequest usersRequest = (UsersRequestBuilder()
  ..limit = 50
  ..searchKeyword = "super"
  ..searchIn = ["uid", "name"]
  ).build();

Set Status

Filters users by online status:
  • CometChatUserStatus.online — Only online users
  • CometChatUserStatus.offline — Only offline users
If not set, returns all users.
UsersRequest usersRequest = (UsersRequestBuilder()
  ..limit = 50
	..userStatus = CometChatUserStatus.online
  ).build(); 

Hide Blocked Users

When true, excludes users blocked by the logged-in user from the results.
UsersRequest usersRequest = (UsersRequestBuilder()
  ..limit = 50
	..hideBlockedUsers = true
  ).build();  

Set Roles

Filters users by specified roles.
List<String> roles = [];
roles.add("role1");
roles.add("role2");
UsersRequest usersRequest = (UsersRequestBuilder()
  ..limit = 50
	..roles = roles
  ).build();

Friends Only

When true, returns only friends of the logged-in user.
UsersRequest usersRequest = (UsersRequestBuilder()
  ..limit = 50
	..friendsOnly = true
  ).build();

Set Tags

Filters users by specified tags.
List<String> tags = [];
tags.add("tag1");
tags.add("tag2");
UsersRequest usersRequest = (UsersRequestBuilder()
  ..limit = 50
	..tags = tags
  ).build();  

With Tags

When true, includes tag data in the returned user objects.
UsersRequest usersRequest = (UsersRequestBuilder()
  ..limit = 50
	..withTags = true
  ).build();

Set UIDs

Fetches specific users by their UIDs. Maximum 25 users per request.
List<String> uids = [];
uids.add("UID1");
uids.add("UID2");
UsersRequest usersRequest = (UsersRequestBuilder()
  ..limit = 25
	..uids = uids
  ).build(); 

Sort By

Sorts the user list by a specific property. Default sort order: status → name → UID. Pass "name" to sort by name → UID.
UsersRequest usersRequest = (UsersRequestBuilder()
  ..limit = 30
  ..sortBy = "name"
  ).build();

Sort By Order

Sets the sort order. Default is ascending ("asc"). Use "desc" for descending.
UsersRequest usersRequest = (UsersRequestBuilder()
  ..limit = 30
  ..sortByOrder = "desc"
  ).build();
After configuring the builder, call build() to get the UsersRequest object, then call fetchNext() to retrieve users.
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}");
  });
The fetchNext() method returns a list of User objects.
On Success — A list of User objects matching the request filters. Each item in the list contains:User Object:
ParameterTypeDescriptionSample Value
uidstringUnique identifier of the user"cometchat-uid-1"
namestringDisplay name of the user"Andrew Joseph"
linkstringProfile linknull
avatarstringAvatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-1.webp"
metadataobjectCustom metadata{}
statusstringOnline status"online"
rolestringUser role"default"
statusMessagestringStatus messagenull
tagsarrayUser tags[]
hasBlockedMebooleanWhether this user has blocked the current userfalse
blockedByMebooleanWhether the current user has blocked this userfalse
lastActiveAtnumberEpoch timestamp of last activity1745554700
ParameterTypeDescriptionSample Value
codestringError code identifier"ERR_CHAT_API_FAILURE"
messagestringHuman-readable error message"Failed to fetch the requested data."
detailsstringAdditional technical details"An unexpected error occurred while processing the request."

Retrieve Particular User Details

Use getUser() to fetch a specific user’s details by UID.
String UID = "UID";
  
CometChat.getUser(UID, onSuccess: (User user){
    debugPrint("User Fetched Successfully : $user");
  }, onError: (CometChatException e){
    debugPrint("User Fetch Failed: ${e.message}");
  });
The getUser() method takes the following parameters:
ParameterDescription
UIDThe 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.
On Success — A User object containing the details of the requested user:User Object:
ParameterTypeDescriptionSample Value
uidstringUnique identifier of the user"cometchat-uid-1"
namestringDisplay name of the user"Andrew Joseph"
linkstringProfile linknull
avatarstringAvatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-1.webp"
metadataobjectCustom metadata{}
statusstringOnline status"online"
rolestringUser role"default"
statusMessagestringStatus messagenull
tagsarrayUser tags[]
hasBlockedMebooleanWhether this user has blocked the current userfalse
blockedByMebooleanWhether the current user has blocked this userfalse
lastActiveAtnumberEpoch timestamp of last activity1745554700
ParameterTypeDescriptionSample Value
codestringError code identifier"ERR_UID_NOT_FOUND"
messagestringHuman-readable error message"The specified UID does not exist."
detailsstringAdditional technical details"Please verify the UID and try again."

Get Online User Count

Use getOnlineUserCount() to get the total number of online users in your app.
CometChat.getOnlineUserCount(onSuccess: (int count){
    debugPrint("Online User Count: $count"); 
  }, onError: (CometChatException e){
    debugPrint("User Count Fetch Failed: ${e.message}");
  });
getOnlineUserCount() resolves with an int representing the total count of currently online users in your app.
On Success — An int value representing the total count of online users:
ParameterTypeDescriptionSample Value
countnumberTotal number of online users12
ParameterTypeDescriptionSample Value
codestringError code identifier"ERR_CHAT_API_FAILURE"
messagestringHuman-readable error message"Failed to fetch the requested data."
detailsstringAdditional technical details"An unexpected error occurred while processing the request."

Next Steps

User Presence

Monitor and manage real-time user online/offline status

Block Users

Block and unblock users to control interactions

User Management

Create, update, and delete users programmatically

Users Overview

Explore all user-related features and capabilities