I have an async stream provider (details omitted for clarity – basically it delivers a stream of Strings):
final mqttStreamProvider = StreamProvider<String>((ref) async* {
final wsClient = ref.watch(mqttClientProvider.future);
final service = await wsClient;
await for (final value in service.getMessageStream()) {
yield value;
}
});
My understanding is that the right way to handle messages that don’t result in a UI impact (no rebuild) is to use :
ref.listen(...)
in the build method of the top level widget of my application.
However, I can’t find any example on how to listen to an async stream provider.
Any help on how to do this much appreciated!
2
Answers
Well, just after asking the answer came :-)
You can
watch
for yourStreamProvider
inside thebuild
methodand then you can use
AsyncValue.when
like this,I believe that in this way you can handle your error and loading state easily.
Also, you can skip loading or error using,