mapbox-maps-flutter icon indicating copy to clipboard operation
mapbox-maps-flutter copied to clipboard

Exception thrown after calling `dispose()` on MapboxMap

Open kalucky0 opened this issue 1 year ago • 9 comments

Environment

  • Flutter version: 3.16.9
  • mapbox_maps_flutter version: 0.5.1
  • Device: iPhone Simulator
  • OS: iOS 17.2

Steps to Reproduce

  • Navigate to a screen with MapWidget
  • Pop the screen with MapWidget Code: GitHub Gist

Description

I'm encountering an issue where an exception is thrown after exiting a screen and calling dispose() on MapboxMap. The exception message suggests that the MapboxMap object is being used after being disposed.

Expected Behavior

After calling dispose() on a MapboxMap object, the object is disposed of properly and no exception is thrown.

Actual Behavior

The following exception is thrown:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: A MapboxMap was used after being disposed.
Once you have called dispose() on a MapboxMap, it can no longer be used.
#0      ChangeNotifier.debugAssertNotDisposed.<anonymous closure> (package:flutter/src/foundation/change_notifier.dart:179:9)
#1      ChangeNotifier.debugAssertNotDisposed (package:flutter/src/foundation/change_notifier.dart:186:6)
#2      ChangeNotifier.dispose (package:flutter/src/foundation/change_notifier.dart:375:27)
#3      MapboxMap.dispose (package:mapbox_maps_flutter/src/mapbox_map.dart:194:11)
#4      _MapWidgetState.dispose (package:mapbox_maps_flutter/src/map_widget.dart:208:18)
<asynchronous suspension>

kalucky0 avatar Feb 13 '24 14:02 kalucky0

Any updates on this issue? I am also having the same issue in my app.

ilyosbekkk avatar Feb 21 '24 15:02 ilyosbekkk

The issue persists in 1.0.0 release

kalucky0 avatar Mar 06 '24 17:03 kalucky0

Have the same issue, anyone any luck in dealing with it?

appinteractive avatar Mar 14 '24 11:03 appinteractive

Same problem here

kmsbin avatar Apr 10 '24 15:04 kmsbin

I solved it in my case by NOT calling Map.dispose(); myself.

appinteractive avatar Apr 10 '24 16:04 appinteractive

Doesn't that break when opening the same screen the second time?

kalucky0 avatar Apr 10 '24 17:04 kalucky0

Doesn't that break when opening the same screen the second time?

Not for me, seems it is already called by the MapBox widget itself, so I have no issues with that. Seems the issue came from disposing it a second time.

appinteractive avatar Apr 10 '24 17:04 appinteractive

When opening the same screen for the second time, there is another problem with symbols.

` I/flutter ( 5458): MissingPluginException(No implementation found for method source#setFeature on channel plugins.flutter.io/mapbox_maps_0) E/flutter ( 5458): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: MissingPluginException(No implementation found for method source#setGeoJson on channel plugins.flutter.io/mapbox_maps_0) E/flutter ( 5458): #0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:332:7) E/flutter ( 5458): E/flutter ( 5458): #1 MethodChannelMapboxGl.setGeoJsonSource (package:mapbox_gl_platform_interface/src/method_channel_mapbox_gl.dart:526:5) E/flutter ( 5458): E/flutter ( 5458): #2 MapboxMapController.setGeoJsonSource (package:mapbox_gl/src/controller.dart:312:5) E/flutter ( 5458): E/flutter ( 5458): #3 AnnotationManager._setAll (package:mapbox_gl/src/annotation_manager.dart:84:7) E/flutter ( 5458): E/flutter ( 5458): #4 AnnotationManager.add (package:mapbox_gl/src/annotation_manager.dart:103:5) E/flutter ( 5458): E/flutter ( 5458): #5 MapboxMapController.addSymbol (package:mapbox_gl/src/controller.dart:624:5) E/flutter ( 5458): E/flutter ( 5458): #6 _ControllerBase.updateTruckSymbol (controller.dart:342:24) E/flutter ( 5458): E/flutter ( 5458): #7 _ControllerBase.setSymbols (controller.dart:414:5) E/flutter ( 5458): E/flutter ( 5458): #8 _ControllerBase.init.. (controller.dart:133:11) E/flutter ( 5458):

` I think the problem is around the mapbox_maps id

Doesn't that break when opening the same screen the second time?

kmsbin avatar Apr 10 '24 17:04 kmsbin

I do this;

late MapboxMap? _mapController;

  @override
  void dispose() {
    locationSubscription?.cancel();
    //_mapController.dispose();
    _mapController = null;

    super.dispose();
  }
  
  //and access with ?
 void getVisibleBounds(){
var visibleBounds = await _mapController?.coordinateBoundsForCamera(cameraState.toCameraOptions());
}

gorkemhacioglu avatar May 23 '24 17:05 gorkemhacioglu