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

时间:2020-10-25 14:16:47

标签: flutter google-cloud-firestore stream-builder

我正在Flutter中使用StreamBuilder来获取Firestore集合users的实时更新。我想按年龄过滤用户。

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

基本上,底层的Firestore查询只是一个简单的where查询。

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

每次用户在UI中更改minAgemaxAge的值时,StreamBuilder都会接收这些新参数并进行更新。每次都会引起新查询吗?如果是,此查询是否每次都会从数据库中获取所有对象,还是仅从本地缓存中获取新对象,而其余部分则获取?

1 个答案:

答案 0 :(得分:1)

是的,每次minAge或maxAge更改时,您的代码都会进行新查询。无法使用动态更改的过滤器更新查询。除非应用程序离线,否则查询将不使用缓存,或者您仅将查询定向到缓存(这不是一个好主意,除非您知道所有可能的文档都已被缓存)。