I have a firebase snapshot StreamProvider
final poiChangesProvider = StreamProvider.autoDispose((ref) {
return db.collection(POI.colPois)
.withConverter<POI>(
fromFirestore: (snapshot, _) => ...,
toFirestore: (poi, _) => ...)
.snapshots();
});
When I ref.watch/listen to it I get all the POIs and then a stream of changes, as expected.
But when I ref.watch it from multiple places, the first one to watch gets all the initial data, while all other watchers only receive the following updates.
How can I make a provider that caches and returns the last streamed data to new listeners?
The riverpod docs for StreamProvider state that it caches the latest value emitted by the stream, ensuring that if a listener is added after an event is emitted, the listener will still have immediate access to the most up-to-date event.
But I have not been able to make this happen.
2
Answers
You can build a provider that watches a stream, and slowly accumulates a snapshot. Something like:
I may have gotten one thing or another wrong here… but the basic concept is you have an accumulator to hold all the values and emit the entire collection each tick.
If you want to emit only the latest, but ensure prior values are also emitted on first contact, there are Stream extensions in package:async that can do that.
StreamProvider keeping track of the last value and offering it to new listeners is the default behavior – assuming your listener is
ref.watch
.If you are talking about
ref.listen
, it has an optionalfireImmediately
parameter: