flutter 如何获取所有聊天中未读消息的总数?

2o7dmzc5  于 2023-05-30  发布在  Flutter
关注(0)|答案(1)|浏览(198)

我试图创建一个徽章,显示在底部NavigationBar的未读消息的总数,但我有一个流,显示在每个聊天未读消息的数量。我怎样才能得到这些数字的和呢?以下是我的流:

@override
  Stream<int> getNumMessageNotseen(ChatEntity chat) {
    return firebaseFirestore
        .collection('chats')
        .doc(chat.cid)
        .collection('messages')
        .orderBy('createAt')
        .snapshots()
        .map((messages) {
      int num = 0;
      int index = 0;

      for (int i = 0; i < chat.participantsid!.length; i++) {
        if (chat.participantsid![i] == chat.uid) {
          index = i;
        }
      }

      for (var doc in messages.docs) {
        MessageModel message = MessageModel.fromSnapshot(doc);
        if (message.senderid != chat.uid) {
          if (message.isSeen![index] == false) {
            num++;
          }
        }
      }
      return num;
    });
  }
92vpleto

92vpleto1#

您可以使用rxdart库中的combine

Stream<int> getTotalNumMessageNotseen(List<ChatEntity> chats) {
  List<Stream<int>> streams = chats.map((chat) => getNumMessageNotseen(chat)).toList();
  return CombineLatestStream.list<int>(streams).map((list) => list.fold<int>(0, (sum, num) => sum + num));
}

相关问题