firebase - 可以从 Firestore 快照 Flutter 获取元数据吗?

标签 firebase dart google-cloud-platform flutter google-cloud-firestore

我需要获取快照 元数据,以便检查写入 Firestore 是否成功。我看source并看到有 SnapshotMetadata 和 bool 值 hasPendingWrites()。但我找不到如何实现。没有开源 dart 项目使用过它。

Firebase doc say可以使用:.onSnapshot/.addSnapshotListener来指定includeMetadataChanges: true

但我需要确保在查询 QuerySnapshot 时获得元数据。我对 stream 使用 query 而不是 addSnapshotListener

像这样:

        child: new FirestoreAnimatedList(
          query: Firestore.instance.collection('Collection')
              .orderBy('timestamp', descending: true)
              .snapshots(),
          padding: new EdgeInsets.all(8.0),
          reverse: true,
          itemBuilder: (_, DocumentSnapshot snapshot,
              Animation<double> animation, int x) {
            return new Chat(
                snapshot: snapshot, animation: animation);
          },
        ),

我尝试指定:

          query: Firestore.instance.collection('Collection')
              .snapshots(includeMetadataChanges: true),

但这不可能:

error: The named parameter 'includeMetadataChanges' isn't defined.

我也试试:

snapshot.getMetadata().hasPendingWrites()

但是报错:

error: The method 'getMetaData' isn't defined for the class 'DocumentSnapshot'.

有谁知道如何在 Flutter 中做到这一点?可能吗?

我试了这么久还是找不到方法..求助!

谢谢!

最佳答案

添加了 includeMetadataChanges 参数

0.12.9 版本的 cloud_firestore 包中添加了 Flutter 对 includeMetadataChanges 参数的支持。

当您调用 snapshots() 函数时,您现在可以将其作为参数包含在内。

此示例返回集合中所有文档的,作为联系人列表。如果 includeMetadataChangesfalse(默认行为),则当元数据更改(例如 hasPendingWritesisFromCache)。如果 true,则流会根据这些更改进行更新。

Stream<List<Contact>> getAllContactsStream() {
    return Firestore.instance.collection('contacts')
    .orderBy('name', descending: false)
    .snapshots(includeMetadataChanges: true)
    .map((snapshot) => snapshot.documents.map((document) => Contact.fromFirestore(document)).toList())
}

对于单个文档快照,可以使用 document.data 访问普通的 Firestore 数据。使用 document.metadata 访问元数据。

关于firebase - 可以从 Firestore 快照 Flutter 获取元数据吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55021607/

相关文章:

flutter - Flutter Fire 中的 Firestore 动态查询

google-cloud-platform - 如何为 GKE 内部 Ingress 设置静态内部 IP

使用 curl 创建时 Firebase 动态链接内部错误

java - 离线时强制从 Cloud Firestore 缓存获取

ios - pod firebase 到 Xcode 时出错

flutter - "This function has a return type of ' 小部件 ', but doesn' 以返回语句结束。”

dart - 使用 dart 从网络上抓取数据

json - 在 flutter 中从 dart 对象创建一个空的 json 对象

javascript - Firebase 云功能。无法读取未定义 (.ref) 的属性父级

node.js - 将文档 ID 作为字段 ID 包含在 firestore 中