rooms method
Returns a stream of rooms from Firebase. Only rooms where current
logged in user exist are returned. orderByUpdatedAt
is used in case
you want to have last modified rooms on top, there are a couple
of things you will need to do though:
- Make sure
updatedAt
exists on all rooms - Write a Cloud Function which will update
updatedAt
of the room when the room changes or new messages come in - Create an Index (Firestore Database -> Indexes tab) where collection ID
is
rooms
, field indexed areuserIds
(type Arrays) andupdatedAt
(type Descending), query scope isCollection
.
Implementation
Stream<List<types.Room>> rooms({bool orderByUpdatedAt = false}) {
final fu = firebaseUser;
if (fu == null) return const Stream.empty();
final collection = orderByUpdatedAt
? getFirebaseFirestore()
.collection(config.roomsCollectionName)
.where('userIds', arrayContains: fu.uid)
.orderBy('updatedAt', descending: true)
: getFirebaseFirestore()
.collection(config.roomsCollectionName)
.where('userIds', arrayContains: fu.uid);
return collection.snapshots().asyncMap(
(query) => processRoomsQuery(
fu,
getFirebaseFirestore(),
query,
config.usersCollectionName,
),
);
}