[英]Flutter Firebase local change doesn't update listener stream
I'm relying on Firebase Firestore offline capabilities, so I'm not using await
on my queries as stated on the Access Data Offline Firebase doc .我依赖 Firebase Firestore 离线功能,所以我没有按照Access Data Offline Firebase 文档中所述对我的查询使用
await
。 I'm expecting that when I write something I'll get an immediate reflection on my read stream, however, I'm only getting an update when the server/remote has been updated.我希望当我写一些东西时,我会立即反映我的阅读 stream,但是,我只在服务器/远程更新时得到更新。 Basically:
基本上:
await
await
_db.doc(parentDoc).collection(DocInnerCollection).doc(childDoc).update({
"name": value,
});
includeMetadataChanges
to true as stated in the above doc.includeMetadataChanges
设置为 true。_db.doc(parentDoc)
.collection(DocInnerCollection)
.orderBy('start_date', 'desc')
.limitToLast(1)
.snapshots(includeMetadataChanges: true)
.map((snapshot) {
print(snapshot.metadata.isFromCache)
});
However, I get no such update and instead I only get an update when the server has been updated.但是,我没有得到这样的更新,而是在服务器更新后才得到更新。
You're requesting only one document with .limitToLast(1)
, yet are not providing a sort order for your query.您只请求一个带有
.limitToLast(1)
的文档,但没有为您的查询提供排序顺序。 This essentially means that you'll get a random document from your collection, and the chances of that being the newly updated document are close to zero.这实质上意味着您将从您的收藏中获得一个随机文档,并且该文档成为新更新文档的可能性接近于零。
If you want the latest (not just last) document, you need some ordering criteria to determine what latest means.如果您想要最新的(不仅仅是最后的)文档,您需要一些排序标准来确定最新的含义。 Typically you'd do this by:
通常你会通过以下方式做到这一点:
lastUpdated
field to your documents, and setting that to firebase.firestore.FieldValue.serverTimestamp()
.lastUpdated
字段添加到您的文档,并将其设置为firebase.firestore.FieldValue.serverTimestamp()
。orderBy('lastUpdated', 'desc')
.orderBy('lastUpdated', 'desc')
在该时间戳上排序您的查询。limit(1)
.limit(1)
限制为第一个结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.