@@ -20,14 +20,17 @@ Future<Map<String, dynamic>> fetchUser(
2020 String usersTableName,
2121 String schema, {
2222 String ? role,
23- }) async =>
24- (await instance
25- .schema (schema)
26- .from (usersTableName)
27- .select ()
28- .eq ('id' , userId)
29- .limit (1 ))
30- .first;
23+ }) async {
24+ final data = (await instance
25+ .schema (schema)
26+ .from (usersTableName)
27+ .select ()
28+ .eq ('id' , userId)
29+ .limit (1 ))
30+ .first;
31+ data['role' ] = role;
32+ return data;
33+ }
3134
3235/// Returns a list of [types.Room] created from Firebase query.
3336/// If room has 2 participants, sets correct room name and image.
@@ -63,30 +66,28 @@ Future<types.Room> processRoomRow(
6366 final type = data['type' ] as String ;
6467 final userIds = data['userIds' ] as List <dynamic >;
6568 final userRoles = data['userRoles' ] as Map <String , dynamic >? ;
66-
67- final users = await Future .wait (
68- userIds.map (
69- (userId) => fetchUser (
70- instance,
71- userId as String ,
72- usersTableName,
73- schema,
74- role: userRoles? [userId] as String ? ,
75- ),
76- ),
77- );
69+ final users = data[ 'users' ] ??
70+ await Future .wait (
71+ userIds.map (
72+ (userId) => fetchUser (
73+ instance,
74+ userId as String ,
75+ usersTableName,
76+ schema,
77+ role: userRoles? [userId] as String ? ,
78+ ),
79+ ),
80+ );
7881
7982 if (type == types.RoomType .direct.toShortString ()) {
80- try {
81- final otherUser = users.firstWhere (
82- (u) => u['id' ] != supabaseUser.id,
83- );
83+ final index = users.indexWhere (
84+ (u) => u['id' ] != supabaseUser.id,
85+ );
86+ if (index >= 0 ) {
87+ final otherUser = users[index];
8488 imageUrl = otherUser['imageUrl' ] as String ? ;
8589 name = '${otherUser ['firstName' ] ?? '' } ${otherUser ['lastName' ] ?? '' }'
8690 .trim ();
87- } catch (e) {
88- // Do nothing if other user is not found, because he should be found.
89- // Consider falling back to some default values.
9091 }
9192 }
9293 data['imageUrl' ] = imageUrl;
0 commit comments