Skip to content

[google_maps_flutter_web] Stop listening to map events when disposed #9250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.5.13

* Stop processing events and cancel subscriptions when controller is disposed.

## 0.5.12

* Adds support for ground overlay.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,44 @@ void main() {
expect(events[4], isA<CameraIdleEvent>());
});

testWidgets('stops listening to map events once disposed',
(WidgetTester tester) async {
controller = createController()
..debugSetOverrides(
createMap: (_, __) => map,
circles: circles,
heatmaps: heatmaps,
markers: markers,
polygons: polygons,
polylines: polylines,
groundOverlays: groundOverlays,
)
..init();

controller.dispose();

// Trigger events on the map, and verify they've been broadcast to the stream
final Stream<MapEvent<Object?>> capturedEvents = stream.stream.take(5);

gmaps.event.trigger(
map,
'click',
gmaps.MapMouseEvent()..latLng = gmaps.LatLng(0, 0),
);
gmaps.event.trigger(
map,
'rightclick',
gmaps.MapMouseEvent()..latLng = gmaps.LatLng(0, 0),
);
// The following line causes 2 events
gmaps.event.trigger(map, 'bounds_changed');
gmaps.event.trigger(map, 'idle');

final List<MapEvent<Object?>> events = await capturedEvents.toList();

expect(events, isEmpty);
});

testWidgets("binds geometry controllers to map's",
(WidgetTester tester) async {
controller = createController()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ class GoogleMapController {
TileOverlaysController? _tileOverlaysController;
GroundOverlaysController? _groundOverlaysController;

StreamSubscription<void>? _onClickSubscription;
StreamSubscription<void>? _onRightClickSubscription;
StreamSubscription<void>? _onBoundsChangedSubscription;
StreamSubscription<void>? _onIdleSubscription;

// Keeps track if _attachGeometryControllers has been called or not.
bool _controllersBoundToMap = false;

Expand Down Expand Up @@ -243,32 +248,46 @@ class GoogleMapController {
void _attachMapEvents(gmaps.Map map) {
map.onTilesloaded.first.then((void _) {
// Report the map as ready to go the first time the tiles load
_streamController.add(WebMapReadyEvent(_mapId));
if (!_streamController.isClosed) {
_streamController.add(WebMapReadyEvent(_mapId));
}
});
map.onClick.listen((gmaps.MapMouseEventOrIconMouseEvent event) {
_onClickSubscription =
map.onClick.listen((gmaps.MapMouseEventOrIconMouseEvent event) {
assert(event.latLng != null);
_streamController.add(
MapTapEvent(_mapId, gmLatLngToLatLng(event.latLng!)),
);
if (!_streamController.isClosed) {
_streamController.add(
MapTapEvent(_mapId, gmLatLngToLatLng(event.latLng!)),
);
}
});
map.onRightclick.listen((gmaps.MapMouseEvent event) {
_onRightClickSubscription =
map.onRightclick.listen((gmaps.MapMouseEvent event) {
assert(event.latLng != null);
_streamController.add(
MapLongPressEvent(_mapId, gmLatLngToLatLng(event.latLng!)),
);
if (!_streamController.isClosed) {
_streamController.add(
MapLongPressEvent(_mapId, gmLatLngToLatLng(event.latLng!)),
);
}
});
map.onBoundsChanged.listen((void _) {
_onBoundsChangedSubscription = map.onBoundsChanged.listen((void _) {
if (!_mapIsMoving) {
_mapIsMoving = true;
_streamController.add(CameraMoveStartedEvent(_mapId));
if (!_streamController.isClosed) {
_streamController.add(CameraMoveStartedEvent(_mapId));
}
}
if (!_streamController.isClosed) {
_streamController.add(
CameraMoveEvent(_mapId, _gmViewportToCameraPosition(map)),
);
}
_streamController.add(
CameraMoveEvent(_mapId, _gmViewportToCameraPosition(map)),
);
});
map.onIdle.listen((void _) {
_onIdleSubscription = map.onIdle.listen((void _) {
_mapIsMoving = false;
_streamController.add(CameraIdleEvent(_mapId));
if (!_streamController.isClosed) {
_streamController.add(CameraIdleEvent(_mapId));
}
});
}

Expand Down Expand Up @@ -588,6 +607,14 @@ class GoogleMapController {
_clusterManagersController = null;
_tileOverlaysController = null;
_groundOverlaysController = null;
_onClickSubscription?.cancel();
_onClickSubscription = null;
_onRightClickSubscription?.cancel();
_onRightClickSubscription = null;
_onBoundsChangedSubscription?.cancel();
_onBoundsChangedSubscription = null;
_onIdleSubscription?.cancel();
_onIdleSubscription = null;
_streamController.close();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: google_maps_flutter_web
description: Web platform implementation of google_maps_flutter
repository: https://github.com/flutter/packages/tree/main/packages/google_maps_flutter/google_maps_flutter_web
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+maps%22
version: 0.5.12
version: 0.5.13

environment:
sdk: ^3.4.0
Expand Down