I am currently working on an app and I'm trying to implement a chat system. I am trying to get the number of unread messages by first checking to see if the chat room is active, and if it is, get the last message sent in each chat room and if it's unread, add to the counter. The issue however, is that when I make another query call inside of the first query, it does not run. This is what I have currently.
static Stream<int> getUnreadMessagesCountStream(String userId) {
final FirebaseFirestore firestore = FirebaseFirestore.instance;
return firestore.collection('chat_rooms').snapshots().map((querySnapshot) {
int unreadCount = 0;
for (QueryDocumentSnapshot<Map<String, dynamic>> document
in querySnapshot.docs) {
Map<String, dynamic> chatSettings = document.data();
if (chatSettings['active'] == true) {
firestore
.collection('chat_rooms')
.doc(document.id)
.collection("messages")
.orderBy('timestamp', descending: true)
.limit(1)
.snapshots()
.map((messagesSnapshot) {
Map<String, dynamic> lastMessage =
messagesSnapshot.docs.first.data();
if (lastMessage['read'] == false &&
lastMessage['receiver_id'] == userId) {
unreadCount++;
}
});
}
}
return unreadCount;
});
}
The first query call return firestore.collection('chat_rooms').snapshots().map((querySnapshot) {
works fine, but the inner call
firestore
.collection('chat_rooms')
.doc(document.id)
.collection("messages")
.orderBy('timestamp', descending: true)
.limit(1)
.snapshots()
.map((messagesSnapshot) {
Map<String, dynamic> lastMessage =
messagesSnapshot.docs.first.data();
if (lastMessage['read'] == false &&
lastMessage['receiver_id'] == userId) {
unreadCount++;
}
});
does not.
My Firebase layout is as followsFirestore layout The messages sub collection is just a collection of documents Messages sub collection
I've tried saving everything in one document, the settings and the messages, but I ran into the problem of messages being dropped if two users sent a message at the same time. I also tried using a StreamBuilder from the async package and Rx. I've also tried using collectionGroup instead of collection but then I wasn't able to access the document with the activity status.
Any help would be much appreciated.