Reputation: 3290
I've been developing this Flutter application in which one widget needs to read a list of Objects from firestore database.
To do so, I've a model for the specific Object (which is called Polizza).
I've a class called DatabaseService which contains the methods for getting the Streams.
In this case, this is the method I use to query the DB and get a stream:
Stream<List<Polizza>> streamPolizze({String id, String filter = ''}) {
var ref = _db
.collection('polizze')
.doc(id)
.collection('elenco')
.orderBy('indice');
if (filter.isNotEmpty) {
List<String> list = filter.split(' ').map((e) => e.toUpperCase());
ref = ref.where('indice', arrayContainsAny: list);
}
return ref.limit(10).snapshots().map(
(list) => list.docs.map((doc) => Polizza.fromFirestore(doc)).toList());
}
In the parent widget I was using a StreamProvider to enable his children to access the List of Polizza:
@override
Widget build(BuildContext context) {
var user = Provider.of<User>(context);
var filter = Provider.of<FilterPolizze>(context);
return MultiProvider(
providers: [
StreamProvider(create: (BuildContext context) => DatabaseService().streamPolizze(id: user.uid))
],
child: ...
);
}
It worked.
But now I want to apply a filter to the query. For doing so I've built a simple TextField widget, which is able to provide me the filter value throught a ChangeNotifierProvider. This works fine, in fact I can read the filter and his updates from Provider.of(context). FilterPolizze is the Object type I've built for the filter.
I've tried to update the StreamProvider like that:
StreamProvider(create: (BuildContext context) => DatabaseService().streamPolizze(id: user.uid, filter: filter.value))
But it seems like it doesn't rebuild.
How can I notify the StreamProvider to rebuild itself if one of his parameters change?
Upvotes: 0
Views: 1327
Reputation: 5648
Give the SteamProvider
a Key
which you change when the filter changes.
So for instance use a key with the value 'polizze${filter.value}'
Upvotes: 4