我需要获取快照
元数据,以便检查写入 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()
函数时,您现在可以将其作为参数包含在内。
此示例返回集合中所有文档的流,作为联系人列表。如果 includeMetadataChanges
为 false(默认行为),则当元数据更改(例如 hasPendingWrites 或 isFromCache强>)。如果 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/