Skip to content

Commit c9a1175

Browse files
committed
fix: add check for groupchats with only 1 member
1 parent 82448ab commit c9a1175

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Change the ChatTranslations constructor to require all translations or use the ChatTranslations.empty constructor if you don't want to specify all translations
88
- Remove the Divider between the users on the new chat screen
99
- Add option to set a custom padding around the list of chats
10+
- Fix nullpointer when firstWhere returns null because there is only 1 person in a groupchat
1011

1112
## 1.4.3
1213

packages/flutter_chat_firebase/lib/service/firebase_chat_overview_service.dart

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,15 @@ class FirebaseChatOverviewService implements ChatOverviewService {
9191
var chat = element.doc.data();
9292
if (chat == null) return;
9393

94-
var otherUser = await _userService.getUser(
95-
chat.users.firstWhere(
96-
(element) => element != currentUser?.id,
97-
),
98-
);
94+
var otherUser = chat.users.any(
95+
(element) => element != currentUser?.id,
96+
)
97+
? await _userService.getUser(
98+
chat.users.firstWhere(
99+
(element) => element != currentUser?.id,
100+
),
101+
)
102+
: null;
99103

100104
var unread =
101105
await _addUnreadChatSubscription(chat.id!, currentUser!.id!);
@@ -144,18 +148,18 @@ class FirebaseChatOverviewService implements ChatOverviewService {
144148
imageUrl: chat.imageUrl ?? '',
145149
unreadMessages: unread,
146150
users: users,
147-
lastMessage: chat.lastMessage != null
151+
lastMessage: chat.lastMessage != null && otherUser != null
148152
? chat.lastMessage!.imageUrl == null
149153
? ChatTextMessageModel(
150-
sender: otherUser!,
154+
sender: otherUser,
151155
text: chat.lastMessage!.text!,
152156
timestamp: DateTime.fromMillisecondsSinceEpoch(
153157
chat.lastMessage!.timestamp
154158
.millisecondsSinceEpoch,
155159
),
156160
)
157161
: ChatImageMessageModel(
158-
sender: otherUser!,
162+
sender: otherUser,
159163
imageUrl: chat.lastMessage!.imageUrl!,
160164
timestamp: DateTime.fromMillisecondsSinceEpoch(
161165
chat.lastMessage!.timestamp

0 commit comments

Comments
 (0)