tlserver / flutter_map_location_marker

A flutter map plugin for displaying device current location.
https://pub.dev/packages/flutter_map_location_marker
BSD 3-Clause "New" or "Revised" License
102 stars 93 forks source link

setState While not mounted causes application to crash #103

Closed ibraheemalayan closed 7 months ago

ibraheemalayan commented 7 months ago

Smartphone

Describe the bug

When going back to a previous page ( with some complex bloc builders ) that has map with the current location marker, the app crashes, probably because a stream listener callback was called and it uses setState on a disposed widget.

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: This widget has been unmounted, so the State no longer has a context (and should be considered defunct).
Consider canceling any active work during "dispose" or using the "mounted" getter to determine if the State is still active.
#0      State.context.<anonymous closure> (package:flutter/src/widgets/framework.dart:951:9)
#1      State.context (package:flutter/src/widgets/framework.dart:957:6)
#2      TickerProviderStateMixin._updateTickerModeNotifier (package:flutter/src/widgets/ticker_provider.dart:328:70)
#3      TickerProviderStateMixin.createTicker (package:flutter/src/widgets/ticker_provider.dart:292:7)
#4      new AnimationController (package:flutter/src/animation/animation_controller.dart:247:21)
#5      _CurrentLocationLayerState._rotateMarker (package:flutter_map_location_marker/src/widgets/current_location_layer.dart:619:40)
#6      _CurrentLocationLayerState._subscriptHeadingStream.<anonymous closure> (package:flutter_map_location_marker/src/widgets/current_location_layer.dart:456:13)
#7      _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#8      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
.
.
.
.
.
#31 .....

Suggested Solution

Maybe wrap the setState in listeners callbacks with an if statement checking the mounted.

tnarik commented 7 months ago

in my case it doesn't crash BUT the same "Unhandled Exception" error appears in logs.

tlserver commented 7 months ago

Fixed in v8.0.5.

ibraheemalayan commented 7 months ago

@tlserver Still not fixed everywhere even in latest release, some places where there is a setState are stilling missing the if statement

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: This widget has been unmounted, so the State no longer has a context (and should be considered defunct).
Consider canceling any active work during “dispose” or using the “mounted” getter to determine if the State is still active.
#0      State.context.<anonymous closure> (package:flutter/src/widgets/framework.dart:951:9)
#1      State.context (package:flutter/src/widgets/framework.dart:957:6)
#2      _CurrentLocationLayerState._subscriptHeadingStream.<anonymous closure> (package:flutter_map_location_marker/src/widgets/current_location_layer.dart:443:14)
#3      _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#4      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#5      _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#6      _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:123:11)
#7      _MapStream._handleData (dart:async/stream_pipe.dart:218:10)
#8      _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
#9      _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#10     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#11     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#12     _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:123:11)
#13     _WhereStream._handleData (dart:async/stream_pipe.dart:195:12)
#14     _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
#15     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#16     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#17     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#18     _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:123:11)
#19     _MapStream._handleData (dart:async/stream_pipe.dart:218:10)
#20     _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
tnarik commented 7 months ago

I can confirm. When running in VS Code it appears as an uncaught exception even if it doesn't crash my app.

When running directly on iPhone, the remaining issue from 8.0.5 described above surfaces as a "FlutterError". In my code I wrap the execution as follows, to be able to see any Exception or Error that might be thrown.

void main() async {
  runZonedGuarded(
    () async {
      WidgetsFlutterBinding.ensureInitialized();
      runApp(MyApp());
    },
    (Object e, StackTrace st) {
      Logger.talker.handle(e, st, 'Uncaught app exception');
    },
  );
}
tnarik commented 7 months ago

brilliant, thanks for 8.0.6, that fixed the Error at least for me.