Reputation: 153
I am using a StreamBuilder in Flutter to get realtime updates of a firestore collection 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.
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. 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?
Upvotes: 0
Views: 525
Reputation: 317372
Yes, your code will make a new query every time minAge or maxAge changes. 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).
Upvotes: 1