Skip to main content

getDirectChatsByUserId method

Future<void> getDirectChatsByUserId ()

This function is used to get all the chats by the user.

parameters required:

  • usedId - current user id, to get all the direct chats associated with this id.

Implementation

Future<void> getDirectChatsByUserId() async {
final userId = _userConfig.currentUser.id;

// trigger graphQL query to get all the chats of the user using [userId].
final String query = ChatQueries().fetchDirectChatsByUserId(userId!);

final result = await _dbFunctions.gqlAuthQuery(query);

final directMessageList = result.data['directChatsByUserID'] as List;

// loop through the result [directMessageList] and append the element to the directChat.
directMessageList.forEach((chat) {
final directChat =
ChatListTileDataModel.fromJson(chat as Map<String, dynamic>);

directChat.users!.forEach((element) {
if (element.id != userId) _chatController.add(directChat);
});
});
}