如何计算flutter中firebasefirestore集合中的消息数量

问题描述 投票:0回答:3

enter image description here我正在尝试在频道的帮助下制作一个聊天应用程序,

有一个搜索页面,我可以在其中搜索要聊天的用户,

如果我点击用户,如果没有找到以前的聊天记录,将会创建一个新窗口,

如果不是第一次聊天,将使用现有的聊天室

代码运行良好,但我想要一些实现,

如果我搜索一个用户并点击他,然后我不聊天就回去,创建的新房间应该被删除......所以我需要消息逻辑的数量......

如何实施才能实现

Future<ChatRoomModel?> getchatroom(UserModel targetuser) async {
    ChatRoomModel? chatroom;

    //here i feel something wrong as even if blocked chatroom, window should be open
    QuerySnapshot querysnapshot = await FirebaseFirestore.instance
        .collection("chatrooms")
        .where("participants.${targetuser.uid}", isEqualTo: true)
        .where("participants.${widget.userModel.uid}", isEqualTo: true)
        .get();

    if (querysnapshot.docs.length > 0) {
      var docdata = querysnapshot.docs[0].data();

      ChatRoomModel existingchatroom =
          ChatRoomModel.fromMap(docdata as Map<String, dynamic>);

      chatroom = existingchatroom;
    } else {
      //creating new chat room
      ChatRoomModel newchatroommodel = ChatRoomModel(
          chatroomid: DateTime.now().toString(),
          participants: {
            widget.userModel.uid.toString(): true,
            targetuser.uid.toString(): true,
          },
          lastMessage: "Say Hi");

      await FirebaseFirestore.instance
          .collection("chatrooms")
          .doc(newchatroommodel.chatroomid)
          .set(newchatroommodel.toMap());
      chatroom = newchatroommodel;
      print("Created success");
    }

    return chatroom;
  }
flutter firebase google-cloud-firestore
3个回答
1
投票

通过“

ChatRoomId
”删除整个聊天记录

FirebaseFirestore.instance
   .collection("chatrooms/'your_chat_room_id'")
          
   .delete()
    .then((value_2) {
   print('========> successfully deleted');

      });

1
投票

通过从“消息”集合中检索文档列表来计算消息数:

QuerySnapshot messages = await querysnapshot.docs[0].reference.collection("messages").get();
int messageCount = messages.size;

0
投票

您可以对对话中的每条消息进行“new_message”计数。更改当前用户和好友的“new_message”。当用户访问时将计数返回 0。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.