'streamBuilder not updating itsself after sorting or filtering - Flutter
I am using StreamBuilder and ListView to show data from FireStoreDatabase in UI. Usually, it updates itself when I make any change in the database (without refresh). but when I sort the data in the stream, it no longer updates. here's the code am using for StreamBuilder.
Stream<List<Attraction>> attractionsStream = Stream.value([]);
void initState() {
final database = Provider.of<Database>(context, listen: false);
setState(() {
//this.attractions = attractions.toList();
attractionsStream = database.attractionStream();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: StreamBuilder<List<Attraction>>(
stream: attractionsStream,
builder: (context, snapshot) {...}
);
}
_sortData() async {
final db = Provider.of<Database>(context, listen: false);
var attractions = await db.attractionStream().first;
attractions.sort((a, b) {...}
setState(() {
attractionsStream = Stream.value(attractions);
});
}
Could you please guide me on this?
Solution 1:[1]
Try adding a unique Key to each widget that your StreamBuilder builds.
It is also uncommon to completely reset the stream, as opposed to pushing new data through the stream already in use.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Craig Labenz |
