getChatByUser method
- ChatUserModel user
override
Retrieves a chat based on the user.
Implementation
@override
Future<PersonalChatModel> getChatByUser(ChatUserModel user) async {
PersonalChatModel? chat;
try {
chat = _chats
.whereType<PersonalChatModel>()
.firstWhere((element) => element.user.id == user.id);
// ignore: avoid_catching_errors
} on StateError {
chat = PersonalChatModel(
user: user,
messages: [],
id: '',
);
chat.id = chat.hashCode.toString();
_chats.add(chat);
debugPrint('New chat created: $chat');
}
_chatsController.add([..._chats]);
notifyListeners();
return chat;
}