简体   繁体   English

Flutter Firebase 本地更改不更新监听器 stream

[英]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:基本上:

  1. Update something in the DB.更新数据库中的内容。 Note, I'm not using await注意,我没有使用await
 _db.doc(parentDoc).collection(DocInnerCollection).doc(childDoc).update({
        "name": value,
      });
  1. I expect my listeners to be updated immediately.我希望我的听众能立即得到更新。 Note I've set the 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:通常你会通过以下方式做到这一点:

  1. Adding a lastUpdated field to your documents, and setting that to firebase.firestore.FieldValue.serverTimestamp() .lastUpdated字段添加到您的文档,并将其设置为firebase.firestore.FieldValue.serverTimestamp()
  2. Ordering your query on that timestamp with orderBy('lastUpdated', 'desc') .使用orderBy('lastUpdated', 'desc')在该时间戳上排序您的查询
  3. And then limiting to the first result with limit(1) .然后使用limit(1)限制为第一个结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter stream builder 不更新 Widget - Flutter stream builder doesn't update Widget 使用 FieldPath 更新字段,更新不接受 FieldPath 作为参数 - Firebase Flutter - Update Field with FieldPath, update doesn't accept FieldPath as parameter - Firebase Flutter 如何监听stream和更新Flutter GridView builder访问Firebase - How to listen to stream and update Flutter GridView builder accessing Firebase 如何在 Flutter 中设置 Firebase 连接的侦听器? - How to setup a listener for Firebase connectivity in Flutter? 使用 Firebase Stream 作为 Flutter 中另一个 Stream 的输入? - Using a Firebase Stream as an input for another Stream in Flutter? 在 Flutter 中处理 Firebase Stream Object - Handling Firebase Stream Object in Flutter 我正在尝试使用 Firebase 在 Flutter 中使用 Google 注销,但它不起作用 - I'm trying to signOut with google in Flutter with Firebase and it doesn't work Flutter Web Firebase Auth 的持久性在 PWA 上不起作用 - Flutter Web Firebase Auth's persistence doesn't work on PWA 使用 Twitter 登录不会将用户添加到 Flutter 上的 Firebase - Logging in with Twitter doesn't add user to Firebase on Flutter Flutter Firebase 存储数据不立即显示数据 - Flutter Firebase Storage Data doesn't show Data immediately
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM