Skip to content
Merged
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.14+1

* Stops processing events and cancels subscriptions when controller is disposed.

## 0.5.14

* Adds support for disabling or moving the camera control button on web.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,46 @@ 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,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 @@ -247,32 +252,48 @@ 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 @@ -648,6 +669,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.14
version: 0.5.14+1

environment:
sdk: ^3.7.0
Expand Down