In my chat list page, number of unread messages is displayed for each chat. And in the bottom navigation bar I want to show total unread messages(sum of all unread in each chat) as a badge next to bottom navigation icon.
First I have to look up 'userChat' to get the list of chatKey, then I should iterate through each 'messages/{chatKey}' to count number of unread.
Problem is that I have to raise the counter only if senderID is NOT the user himself. But there is no orderBy('senderID').notEqualTo(_uid). How can I select items NOT MATCHING certain value?
My code is shown below.
Stream<int> numberOfTotalUnread() {
int unread = 0;
return _userChatDB.child(_uid).onValue.map((event) {
unread = 0;
if(event.snapshot.value != null) {
for (var element in event.snapshot.children) {
print(element.key);
// this is the problem part
_messageDB.child(element.key!).orderByChild('senderID').notEqualTo(_uid).onValue.map((ev) {
if(ev.snapshot.value != null) {
print('??');
for (var el in ev.snapshot.children) {
print(el.key);
if(Map<String, dynamic>.from(el.value as dynamic)['read'] != null) unread += 1;
}
}
});
}
}
print(unread);
return unread;
});
}