简体   繁体   English

使用 Flutter 和 Firestore 中的新数据更新 StreamBuilder

[英]Updating a StreamBuilder with new data in Flutter and Firestore

I am using a StreamBuilder in Flutter to get realtime updates of a firestore collection users .我在 Flutter 中使用 StreamBuilder 来获取 firestore 集合users的实时更新。 I want to filter the users by age.我想按年龄过滤用户。

StreamBuilder<List<User>>(
    stream: UserList(minAge, maxAge),
    builder: (context, snapshot) {
        print(snapshot.data);
    }
);

Basically the underlying firestore query is just a simple where query.基本上,底层的 firestore 查询只是一个简单的where查询。

FirebaseFirestore.instance
        .collection('users')
        .where('age', isGreaterThan: minAge)
        .where('age', isLessThan: maxAge)
        .snapshots();

Every time the user changes the values of minAge or maxAge in the UI, the StreamBuilder receives those new parameters and gets updated.每次用户在 UI 中更改minAgemaxAge的值时,StreamBuilder 都会收到这些新参数并进行更新。 Does this cause a new query every time?这是否每次都会导致新的查询? If yes, will this query fetch all objects from the database every time or will that fetch only new objects and the rest from my local cache?如果是,这个查询是每次都从数据库中获取所有对象,还是只从我的本地缓存中获取新对象和 rest?

Yes, your code will make a new query every time minAge or maxAge changes.是的,每次 minAge 或 maxAge 更改时,您的代码都会进行新查询。 The query cannot be updated with dynamically changing filters.无法使用动态更改的过滤器更新查询。 The query will not use the cache unless the app is offline, or you direct the query at the cache only (which is not a good idea unless you know that all possible documents are already cached).除非应用程序处于离线状态,否则查询不会使用缓存,或者您仅将查询定向到缓存(这不是一个好主意,除非您知道所有可能的文档都已缓存)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM