【问题标题】:how to get nested collections data from firestore如何从firestore获取嵌套集合数据
【发布时间】:2021-01-18 23:01:17
【问题描述】:

我有一个如下所示的 Firestore 集合。我需要获取最后存储的message

Firestore-root
  |
  --- chat_rooms(collection)
        |
        --- chats(collection, the id for that collection is sender_reciever)
             |
             --- time_send: (millisecondsSinceEpoch)
             |
             --- sender:(string)
             |
             --- message:(string)



这是我的db 获取最后一个消息的方法

  getLastMessage(String chatRoomId) {
    return  Firestore.instance.
    collection('chat_rooms').document(chatRoomId)
        .collection('chat').orderBy('time_send',descending: false)
        .limit(1).get();
  }

我在这里称呼它。 Chats 是一个返回 senderlast_message 的小部件。基本上我想做的是,例如,在使用whatsapp时,最后一条消息会弹出主页。我正在尝试做完全相同的事情。这样,我也可以获得username。下面的方法不返回实际的用户数据。因为集合chat_rooms_id 的id 是username_senderusername_reciever 的组合。我只是删除了当前用户的reciever。并且,sender 仍然存在。

   return Chats(
                      username: snapshot.data.documents[index]
                          .data["chat_room_id"] // return chat_room id
                          .toString()
                          .replaceAll("_", "")
                          .replaceAll(Constants.signedUserName, ""),
                      chatRoomId:
                          snapshot.data.documents[index].data["chat_room_id"],
                      last_message: __db
                          .getLastMessage(snapshot
                              .data.documents[index].data[snapshot.data.documents[index]
                                  .data['chat_room_id'].toString()]
                              .toString()).toString()
                    );

结果是

【问题讨论】:

  • 嘿,你能不能也包括Chats()的定义以及运行return chats()的方法是如何定义的?根据消息Instance of future,似乎缺少一些async/awaitthen 关键字。以上是 [1] 中描述的预期行为。 [1]dart.dev/codelabs/…

标签: flutter dart google-cloud-firestore


【解决方案1】:

首先,创建一个类来存储聊天信息

class Chat {
  final String id;
  final time_send;
  final String sender;
  final String message;
  
  Chat({this.id, this.time_send, this.sender, this.message});
  
  
  static Chat fromSnapshot(QuerySnapshot snap) {
    return Chat(
      id: snap.id,
      time_send: snap.get('time_send'),
      sender: snap.get('sender'),
      message: snap.get('message'),
    );
  }
}

然后,如下修改您的 Firestore 查询,使用 snapshots() 而不是 get() 方法

Stream<Chat> chat(String chatRoomId) {
  return  Firestore.instance.
    collection('chat_rooms').document(chatRoomId)
    .collection('chat').orderBy('time_send',descending: false)
    .limit(1)
    .snapshots()
    .map<Chat>((snapshot) {
       if (snapshot.size > 0) {
          return snapshot.docs.map((e) => Chat.fromSnapshot(e)).single;
       } 
 });

【讨论】:

  • 我遇到了类似聊天类中的错误 id, sender, time_sed, message 没有为 fromSnapshot() 方法定义。在查询方法中也存在一些single 没有定义的问题。我的firestore版本是`cloud_firestore: ^0.13.0+1`
  • 我修改了 return snapshot.documents. map((e) =&gt; Chat.fromSnapshot(e)).single 这样的方法,现在它说 DocumentSnapshot cannot assign to Query snapshot for e
  • Chat 类中的 fromSnapshot(QuerySnapshot snap) 为 fromSnapshot(DocumentSnapshot snap) 方法
猜你喜欢
  • 2020-04-29
  • 2020-06-03
  • 1970-01-01
  • 1970-01-01
  • 2019-07-16
  • 2020-11-26
  • 2021-10-31
  • 1970-01-01
  • 2020-09-09
相关资源
最近更新 更多