Skip to content

Commit ecc91ce

Browse files
committed
Cancel map stream subscriptions on dispose
1 parent c853830 commit ecc91ce

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

packages/google_maps_flutter/google_maps_flutter_web/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.5.13
2+
3+
* Stop processing events and cancel subscriptions when controller is disposed.
4+
15
## 0.5.12
26

37
* Adds support for ground overlay.

packages/google_maps_flutter/google_maps_flutter_web/lib/src/google_maps_controller.dart

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ class GoogleMapController {
137137
TileOverlaysController? _tileOverlaysController;
138138
GroundOverlaysController? _groundOverlaysController;
139139

140+
StreamSubscription<void>? _onClickSubscription;
141+
StreamSubscription<void>? _onRightClickSubscription;
142+
StreamSubscription<void>? _onBoundsChangedSubscription;
143+
StreamSubscription<void>? _onIdleSubscription;
144+
140145
// Keeps track if _attachGeometryControllers has been called or not.
141146
bool _controllersBoundToMap = false;
142147

@@ -247,23 +252,25 @@ class GoogleMapController {
247252
_streamController.add(WebMapReadyEvent(_mapId));
248253
}
249254
});
250-
map.onClick.listen((gmaps.MapMouseEventOrIconMouseEvent event) {
255+
_onClickSubscription =
256+
map.onClick.listen((gmaps.MapMouseEventOrIconMouseEvent event) {
251257
assert(event.latLng != null);
252258
if (!_streamController.isClosed) {
253259
_streamController.add(
254260
MapTapEvent(_mapId, gmLatLngToLatLng(event.latLng!)),
255261
);
256262
}
257263
});
258-
map.onRightclick.listen((gmaps.MapMouseEvent event) {
264+
_onRightClickSubscription =
265+
map.onRightclick.listen((gmaps.MapMouseEvent event) {
259266
assert(event.latLng != null);
260267
if (!_streamController.isClosed) {
261268
_streamController.add(
262269
MapLongPressEvent(_mapId, gmLatLngToLatLng(event.latLng!)),
263270
);
264271
}
265272
});
266-
map.onBoundsChanged.listen((void _) {
273+
_onBoundsChangedSubscription = map.onBoundsChanged.listen((void _) {
267274
if (!_mapIsMoving) {
268275
_mapIsMoving = true;
269276
if (!_streamController.isClosed) {
@@ -276,7 +283,7 @@ class GoogleMapController {
276283
);
277284
}
278285
});
279-
map.onIdle.listen((void _) {
286+
_onIdleSubscription = map.onIdle.listen((void _) {
280287
_mapIsMoving = false;
281288
if (!_streamController.isClosed) {
282289
_streamController.add(CameraIdleEvent(_mapId));
@@ -600,6 +607,14 @@ class GoogleMapController {
600607
_clusterManagersController = null;
601608
_tileOverlaysController = null;
602609
_groundOverlaysController = null;
610+
_onClickSubscription?.cancel();
611+
_onClickSubscription = null;
612+
_onRightClickSubscription?.cancel();
613+
_onRightClickSubscription = null;
614+
_onBoundsChangedSubscription?.cancel();
615+
_onBoundsChangedSubscription = null;
616+
_onIdleSubscription?.cancel();
617+
_onIdleSubscription = null;
603618
_streamController.close();
604619
}
605620
}

packages/google_maps_flutter/google_maps_flutter_web/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: google_maps_flutter_web
22
description: Web platform implementation of google_maps_flutter
33
repository: https://github.com/flutter/packages/tree/main/packages/google_maps_flutter/google_maps_flutter_web
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+maps%22
5-
version: 0.5.12
5+
version: 0.5.13
66

77
environment:
88
sdk: ^3.4.0

0 commit comments

Comments
 (0)