getConversationFromMessage static method
Retrieves the Conversation object associated with the given message
.
The message
parameter represents the BaseMessage for which the corresponding Conversation is required.
Returns a Future that resolves to the Conversation associated with the message
.
Returns null
if an error occurs during the retrieval process or if the logged-in user is null.
Implementation
static Future<Conversation?> getConversationFromMessage(
BaseMessage message) async {
try {
String conversationType = message.receiverType;
User? loggedInUser = await CometChat.getLoggedInUser();
String? conversationId = message.conversationId;
if (loggedInUser != null) {
AppEntity appEntity = AppEntity();
if (conversationType == CometChatReceiverType.user) {
if (loggedInUser.uid == message.sender!.uid) {
appEntity = message.receiver!;
} else {
appEntity = message.sender!;
}
} else {
appEntity = message.receiver!;
}
Conversation conversation = Conversation(
conversationId: conversationId,
conversationType: conversationType,
conversationWith: appEntity,
lastMessage: message,
updatedAt: message.updatedAt);
return conversation;
} else {
return null;
}
} on PlatformException catch (platformException) {
debugPrint("Error: $platformException");
} catch (e) {
debugPrint("Error: $e");
}
return null;
}