getUserChats method

Stream<List<ChatSummary>> getUserChats(
  1. String userId
)

Get a stream of chat summaries for a specific user

Implementation

Stream<List<ChatSummary>> getUserChats(String userId) {
  return _firestore
      .collection('chats')
      .where('users', arrayContainsAny: [userId])
      .snapshots()
      .map((snapshot) {
        final list = snapshot.docs.map((doc) {
          final data = doc.data().log('snapshot');

          return ChatSummary.fromMap(
            data,
            userId,
          );
        }).toList()
          ..sort((a, b) => b.lastMessageTime
              .compareTo(a.lastMessageTime)); // Sort by last message time
        return list;
      });
}