最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

flutter - Getting Exception "emit was called after an event handler completed normally." using GeoLocationstre

programmeradmin4浏览0评论

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!

发布评论

评论列表(0)

  1. 暂无评论