Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 从firestore获取最后一条消息_Flutter_Dart_Google Cloud Firestore - Fatal编程技术网

Flutter 从firestore获取最后一条消息

Flutter 从firestore获取最后一条消息,flutter,dart,google-cloud-firestore,Flutter,Dart,Google Cloud Firestore,我有一个firestore收藏,如下所示。我需要获取上次存储的消息 Firestore-root | --- chat_rooms(collection) | --- chats(collection, the id for that collection is sender_reciever) | --- time_send: (millisecondsSinceEpoch)

我有一个firestore收藏,如下所示。我需要获取上次存储的
消息

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);
  }
我在这里叫它
Chats
是一个小部件,它返回
sender
last\u消息
。基本上,我想做的是,例如,在使用whatsapp时,最后一条消息会在主页上弹出。我正试图做完全相同的事情。这样,我也可以得到
用户名。下面的方法不返回实际的用户数据。由于集合
chat\u rooms\u id
具有一个id,该id是
发送方
接收方
的组合
用户名
。我只是删除了当前用户的
接收者
。而
发送者
仍然存在

   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["chat_room_id"]
                              .toString())
                          .data[0]['message']
                          .toString(),
                    );

你需要其中之一

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

…虽然我会注意到这会让你得到一个必须要处理的承诺。您定义了对上一个文档的查询,但没有获取查询结果

我很确定您需要
降序:true
来获取
集合('chat')中的最新值。orderBy('time_send',降序:false)。限制(1)
@FrankvanPuffelen仍然会出现这样的错误
类“Query”没有实例getter“data”。接收者:“查询”的实例尝试调用:数据
错误出现在最后一个代码
最后一条消息
部分,这是因为您没有对构建的查询调用
获取
(或
。快照
),LeadDreamer也在下面提到。有一个程序,但仍然不起作用。我不再收到任何错误,但结果是打印出“未来”的
Insatnce