diff --git a/mapbox_gl_platform_interface/lib/src/callbacks.dart b/mapbox_gl_platform_interface/lib/src/callbacks.dart index f2d084a8e..4813bdfff 100644 --- a/mapbox_gl_platform_interface/lib/src/callbacks.dart +++ b/mapbox_gl_platform_interface/lib/src/callbacks.dart @@ -46,6 +46,11 @@ class ArgumentCallbacks { _callbacks.remove(callback); } + /// Removes all callbacks + void clear() { + _callbacks.clear(); + } + /// Whether this collection is empty. bool get isEmpty => _callbacks.isEmpty; diff --git a/mapbox_gl_platform_interface/lib/src/mapbox_gl_platform_interface.dart b/mapbox_gl_platform_interface/lib/src/mapbox_gl_platform_interface.dart index 25e0c4d85..540e138de 100644 --- a/mapbox_gl_platform_interface/lib/src/mapbox_gl_platform_interface.dart +++ b/mapbox_gl_platform_interface/lib/src/mapbox_gl_platform_interface.dart @@ -136,5 +136,23 @@ abstract class MapboxGlPlatform { Future addSource(String sourceId, SourceProperties properties); - void dispose() {} + @mustCallSuper + void dispose() { + // clear all callbacks to avoid cyclic refs + onInfoWindowTappedPlatform.clear(); + onFeatureTappedPlatform.clear(); + onFeatureDraggedPlatform.clear(); + onCameraMoveStartedPlatform.clear(); + onCameraMovePlatform.clear(); + onCameraIdlePlatform.clear(); + onMapStyleLoadedPlatform.clear(); + + onMapClickPlatform.clear(); + onMapLongClickPlatform.clear(); + onAttributionClickPlatform.clear(); + onCameraTrackingChangedPlatform.clear(); + onCameraTrackingDismissedPlatform.clear(); + onMapIdlePlatform.clear(); + onUserLocationUpdatedPlatform.clear(); + } } diff --git a/mapbox_gl_web/lib/src/mapbox_web_gl_platform.dart b/mapbox_gl_web/lib/src/mapbox_web_gl_platform.dart index 2f839d036..0c39e9d2a 100644 --- a/mapbox_gl_web/lib/src/mapbox_web_gl_platform.dart +++ b/mapbox_gl_web/lib/src/mapbox_web_gl_platform.dart @@ -36,6 +36,12 @@ class MapboxWebGlPlatform extends MapboxGlPlatform viewType: 'plugins.flutter.io/mapbox_gl_${this.hashCode}'); } + @override + void dispose() { + super.dispose(); + _map.remove(); + } + void _registerViewFactory(Function(int) callback, int identifier) { // ignore: undefined_prefixed_name ui.platformViewRegistry.registerViewFactory(