I am using Geolocation package with flutter and in order to receive real-time position data I am using it`s build-in-stream with Bloc state management:
Error-Message:
E/flutter ( 2681): emit was called after an event handler completed normally.
E/flutter ( 2681): This is usually due to an unawaited future in an event handler.
E/flutter ( 2681): Please make sure to await all asynchronous operations with event
handlers
E/flutter ( 2681): and use emit.isDone after asynchronous operations before calling
emit() to
E/flutter ( 2681): ensure the event handler has not completed.
E/flutter ( 2681):
E/flutter ( 2681): **BAD**
E/flutter ( 2681): on<Event>((event, emit) {
E/flutter ( 2681): future.whenComplete(() => emit(...));
E/flutter ( 2681): });
E/flutter ( 2681):
E/flutter ( 2681): **GOOD**
E/flutter ( 2681): on<Event>((event, emit) async {
E/flutter ( 2681): await future.whenComplete(() => emit(...));
E/flutter ( 2681): });
Code:
void _getPositionStream(StartStream event,Emitter<GeoLocatorState> emit)async{
StreamSubscription? positionStream;
positionStream = Geolocator.getPositionStream(locationSettings:
locationSettings).listen((Position position)async {
speed = position.speed;
emit(ProvideSpeedData(speed));//Throws the mentioned exception
});
}
What I don't understand, when I use the stream without bloc directly in the widgetstate I can easily access the data as expected in the documentation, so why I get this error? Basically I understand the meaning of the error but I don`t know how to solve it.
Thanks!