@@ -68,29 +68,30 @@ class FirebaseChatCore {
6868 types.User otherUser, {
6969 Map <String , dynamic >? metadata,
7070 }) async {
71- if (firebaseUser == null ) return Future .error ('User does not exist' );
71+ final fu = firebaseUser;
72+
73+ if (fu == null ) return Future .error ('User does not exist' );
7274
7375 final query = await FirebaseFirestore .instance
7476 .collection ('rooms' )
75- .where ('userIds' , arrayContains: firebaseUser ! .uid)
77+ .where ('userIds' , arrayContains: fu .uid)
7678 .get ();
7779
78- final rooms = await processRoomsQuery (firebaseUser ! , query);
80+ final rooms = await processRoomsQuery (fu , query);
7981
8082 try {
8183 return rooms.firstWhere ((room) {
8284 if (room.type == types.RoomType .group) return false ;
8385
8486 final userIds = room.users.map ((u) => u.id);
85- return userIds.contains (firebaseUser! .uid) &&
86- userIds.contains (otherUser.id);
87+ return userIds.contains (fu.uid) && userIds.contains (otherUser.id);
8788 });
8889 } catch (e) {
8990 // Do nothing if room does not exist
9091 // Create a new room instead
9192 }
9293
93- final currentUser = await fetchUser (firebaseUser ! .uid);
94+ final currentUser = await fetchUser (fu .uid);
9495 final users = [types.User .fromJson (currentUser), otherUser];
9596
9697 final room = await FirebaseFirestore .instance.collection ('rooms' ).add ({
@@ -163,13 +164,15 @@ class FirebaseChatCore {
163164
164165 /// Returns a stream of changes in a room from Firebase
165166 Stream <types.Room > room (String roomId) {
166- if (firebaseUser == null ) return const Stream .empty ();
167+ final fu = firebaseUser;
168+
169+ if (fu == null ) return const Stream .empty ();
167170
168171 return FirebaseFirestore .instance
169172 .collection ('rooms' )
170173 .doc (roomId)
171174 .snapshots ()
172- .asyncMap ((doc) => processRoomDocument (doc, firebaseUser ! ));
175+ .asyncMap ((doc) => processRoomDocument (doc, fu ));
173176 }
174177
175178 /// Returns a stream of rooms from Firebase. Only rooms where current
@@ -183,20 +186,22 @@ class FirebaseChatCore {
183186 /// is `rooms` , field indexed are `userIds` (type Arrays) and `updatedAt`
184187 /// (type Descending), query scope is `Collection`
185188 Stream <List <types.Room >> rooms ({bool orderByUpdatedAt = false }) {
186- if (firebaseUser == null ) return const Stream .empty ();
189+ final fu = firebaseUser;
190+
191+ if (fu == null ) return const Stream .empty ();
187192
188193 final collection = orderByUpdatedAt
189194 ? FirebaseFirestore .instance
190195 .collection ('rooms' )
191- .where ('userIds' , arrayContains: firebaseUser ! .uid)
196+ .where ('userIds' , arrayContains: fu .uid)
192197 .orderBy ('updatedAt' , descending: true )
193198 : FirebaseFirestore .instance
194199 .collection ('rooms' )
195- .where ('userIds' , arrayContains: firebaseUser ! .uid);
200+ .where ('userIds' , arrayContains: fu .uid);
196201
197202 return collection
198203 .snapshots ()
199- .asyncMap ((query) => processRoomsQuery (firebaseUser ! , query));
204+ .asyncMap ((query) => processRoomsQuery (fu , query));
200205 }
201206
202207 /// Sends a message to the Firestore. Accepts any partial message and a
0 commit comments