Hey Felix, is my implementation good? I am trying to implement a functionality wherein it will call the API for every N seconds and using the dart:async Timer class. Like the example below.
So far, this is what I have tried and it is working so far.
@override
Stream<AppConnectionState> mapEventToState(ConnectionEvent event) async* {
if (event is Initializing) {
yield ConnectionUninitialized();
yield* _startConnectionPolling();
}
...
}
Stream<AppConnectionState> _startConnectionPolling() async* {
timer = Timer.periodic(Duration(seconds: 10), (timer) async {
var apiRes = await apiProvider.retrieveDetails();
if (apiRes.connected)
dispatch(Connected());
else
dispatch(Disconnected());
});
}
@override
void dispose() {
timer?.cancel();
super.dispose();
}
Please advice. Thanks!
Hi @csarigumba 馃憢
Thanks for opening an issue!
I would not recommend implementing polling this way because by using yield* within mapEventToState you are blocking the processing of all other incoming events.
Instead, I would recommend taking a similar approach to the one described in the flutter timer tutorial.
Hope that helps 馃憤
Most helpful comment
Hi @csarigumba 馃憢
Thanks for opening an issue!
I would not recommend implementing polling this way because by using
yield*withinmapEventToStateyou are blocking the processing of all other incoming events.Instead, I would recommend taking a similar approach to the one described in the flutter timer tutorial.
Hope that helps 馃憤