简体   繁体   English

数据更改时,flutter StreamBuilder 中使用的 Firestore 快照永远不会更新

[英]Firestore snapshot used in flutter StreamBuilder never gets updated when data changes

I'm a noob with flutter, trying to work with some examples.我是一个颤振的菜鸟,试图用一些例子来工作。 I created a widget that uses a StreamBuilder to pull some query from Firebase.我创建了一个小部件,它使用 StreamBuilder 从 Firebase 中提取一些查询。 It works great, but it never updates the UI when the data changes.它工作得很好,但是当数据更改时它永远不会更新 UI。 I tried reading about it and from all I can see this should work out of the box.我试着阅读它,从我所看到的一切来看,这应该是开箱即用的。 Is there some step I'm missing on the Firebase side to enable the changes to be pushed?我在 Firebase 方面缺少某些步骤来启用更改吗? Or does the SteamBuilder somehow needs to be configured so it pulls for data every once in a while?或者 SteamBuilder 是否需要以某种方式进行配置,以便它每隔一段时间提取一次数据? The code is basic, just copy and paste from some examples:代码是基本的,只是从一些例子中复制和粘贴:

@override
  Widget build(BuildContext context) {
    return StreamBuilder(
      stream: Firestore.instance
        .collection("XXX").orderBy('YYY')
        .where("userID", isEqualTo: userID)
        .snapshots(),
      builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (!snapshot.hasData) {
            return Center(
              child: CircularProgressIndicator(
                valueColor: new AlwaysStoppedAnimation<Color>(
                  Color.fromRGBO(212, 20, 15, 1.0),
                ),
              ),
            );
          } else {
            // Return some list view using the data in snapshot.data.documents
          }
        }
      );
    }

[Update]: seems like my data is not updated even when I change the query. [更新]:即使我更改查询,我的数据似乎也没有更新。 I guess it's using some locally cached data and is not fetching it again from the server.我猜它正在使用一些本地缓存的数据并且不会再次从服务器获取它。

[Update 2]: when I use a query that resolves to no results (eg non-existing collection) the spinning wheel just keeps going. [更新 2]:当我使用解析为没有结果(例如不存在的集合)的查询时,旋转轮会继续运行。 When running again the correct query it goes back to the same old results, not reflecting any changes in the Firebase cloud.再次运行正确的查询时,它会返回到相同的旧结果,不会反映 Firebase 云中的任何更改。

[Update 3]: the problem seems to do with my stream containing orderBy. [更新 3]:问题似乎与我的包含 orderBy 的流有关。 As soon as I remove that everything works fine.一旦我删除一切正常。 Is that a bug??那是bug吗??

you need to create an index utilizing the where and orderBy fields:您需要使用whereorderBy字段创建索引:

https://firebase.google.com/docs/firestore/query-data/indexing https://firebase.google.com/docs/firestore/query-data/indexing

{
  "collectionGroup": "XXX",
  "queryScope": "COLLECTION",
  "fields": [
    {
      "fieldPath": "userId",
      "order": "ASCENDING"
    },
    {
      "fieldPath": "YYY",
      "order": "DESCENDING"
    }
  ]
},

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

相关问题 Flutter:更新单个文档时,StreamBuilder 获取其他 Firestore 文档 - Flutter: StreamBuilder gets other firestore documents when updating a single document Flutter:将 firestore 快照转换为 streambuilder 中的列表 - Flutter: Convert firestore snapshot to list in streambuilder StreamBuilder 快照没有数据 Flutter - StreamBuilder snapshot has no data Flutter 当侦听Firestore快照时,StreamBuilder挂起 - StreamBuilder hangs when listening to Firestore snapshot Flutter - 更新子句参数时 Firestore 流快照,其中不流式传输 - Flutter - Firestore stream snapshot with where not streaming when clause arguments are updated 从 StreamBuilder BloC firestore Flutter 获取数据时出错 - Error when getting data from StreamBuilder BloC firestore Flutter 快照在 StreamBuilder flutter 中总是返回空数据 - snapshot returns always empty data in StreamBuilder flutter Flutter Firestore - 流构建器中的流构建器 - Flutter Firestore - Streambuilder within a streambuilder 如何使用 StreamBuilder 从 firebase firestore 检索数据到 flutter 并使用 ListView.builder 显示值? - How to retrieve data from firebase firestore into flutter using StreamBuilder and also used ListView.builder to show the values? 在 Flutter 中从 Firebase FireCloud 接收数据时,不会执行 StreamBuilder 中的快照代码 - Snapshot code inside StreamBuilder does not get executed when receiving data from Firebase FireCloud in Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM