【问题标题】:Firestore does not return all elements instead it is returning only the updated element, whenever I make an update to a document in a collection每当我对集合中的文档进行更新时,Firestore 不会返回所有元素,而是仅返回更新后的元素
【发布时间】:2019-06-26 16:12:15
【问题描述】:

我正在监听数据库中的更改并使用 Streambuilder 来显示它,当我导航到小部件以及重建小部件时一切正常,但每当我在小部件上并更新文档时,新数据就会已发送,但我只收到我更新的文档,而不是集合中的整个文档。

假设我有四个文档,并更新第二个文档,我只看到第二个文档,要再次查看所有文档,我将不得不离开屏幕并返回

这是我获取所有文档的功能 ==>

Stream<QuerySnapshot> fetchAllConversations(String userId) {
    var firestore = Firestore.instance;
    return firestore
        .collection(userNode)
        .document(userId)
        .collection(conversationNode)
        .orderBy("lastMessageDate", descending: true)
        .snapshots();
  }

//here is my stream builder for listening to the collection

StreamBuilder<QuerySnapshot>(
              stream: convoDao.fetchAllConversations(session.currentUserId),
              builder: (context, stream) {
                if (stream.hasData) {
                  if (stream.data.documentChanges.isEmpty) {
                    return Container(
                        alignment: Alignment.center,
                        child: Text("No active conversations"));
                  }

                  return ListView.builder(
                      itemCount: stream.data.documentChanges.length,
                      itemBuilder: (context, index) {
                        return ActiveChatListItem(convo: ConvoDTO.fromJson(                      stream.data.documentChanges[index].document.data));
                      });
                }

                if (stream.hasError) { //todo: handle error
                }

                return loading();
              })),

我怎样才能继续获取集合中的整个文档。

【问题讨论】:

    标签: firebase dart flutter google-cloud-firestore


    【解决方案1】:

    用户 stream.data.documents 而不是 stream.data.documentChanges

    documentChanges 的文档注释

    /// 自上次快照以来更改的文档数组。如果这
    /// 是第一个快照,所有文档都将在列表中添加更改

    【讨论】:

    • 非常感谢,我不敢相信我错过了这样的事情......我需要长时间休息
    • 不客气!.. 有些日子就是这样。在推特上关注我@nonybrighto。可能很快就会来到埃努古
    猜你喜欢
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    相关资源
    最近更新 更多