Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[google_maps_flutter] Call platform.dispose on GoogleMap widget dispose. #2909

Merged
merged 6 commits into from
Aug 12, 2020
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
5 changes: 5 additions & 0 deletions packages/google_maps_flutter/google_maps_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.5.30

* Add a `dispose` method to the controller to let the native side know that we're done with said controller.
* Call `controller.dispose()` from the `dispose` method of the `GoogleMap` widget.

## 0.5.29+1

* (ios) Pin dependency on GoogleMaps pod to `< 3.10`, to address https://github.com/flutter/flutter/issues/63447
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,9 @@ class GoogleMapController {
Future<Uint8List> takeSnapshot() {
return _googleMapsFlutterPlatform.takeSnapshot(mapId: mapId);
}

/// Disposes of the platform resources
void dispose() {
_googleMapsFlutterPlatform.dispose(mapId: mapId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,13 @@ class _GoogleMapState extends State<GoogleMap> {
_circles = keyByCircleId(widget.circles);
}

@override
void dispose() async {
super.dispose();
GoogleMapController controller = await _controller.future;
controller.dispose();
}

@override
void didUpdateWidget(GoogleMap oldWidget) {
super.didUpdateWidget(oldWidget);
Expand Down
4 changes: 2 additions & 2 deletions packages/google_maps_flutter/google_maps_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
name: google_maps_flutter
description: A Flutter plugin for integrating Google Maps in iOS and Android applications.
homepage: https://github.com/flutter/plugins/tree/master/packages/google_maps_flutter/google_maps_flutter
version: 0.5.29+1
version: 0.5.30

dependencies:
flutter:
sdk: flutter
flutter_plugin_android_lifecycle: ^1.0.0
google_maps_flutter_platform_interface: ^1.0.1
google_maps_flutter_platform_interface: ^1.0.4

dev_dependencies:
flutter_test:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ void main() {
setUp(() {
// Use a mock platform so we never need to hit the MethodChannel code.
GoogleMapsFlutterPlatform.instance = platform;
when(platform.buildView(any, any, any)).thenReturn(Container());
resetMockitoState();
_setupMock(platform);
});

testWidgets('_webOnlyMapCreationId increments with each GoogleMap widget', (
Expand Down Expand Up @@ -61,4 +62,60 @@ void main() {
),
]);
});

testWidgets('Calls platform.dispose when GoogleMap is disposed of', (
WidgetTester tester,
) async {
await tester.pumpWidget(GoogleMap(
initialCameraPosition: CameraPosition(
target: LatLng(43.3608, -5.8702),
),
));

// Now dispose of the map...
await tester.pumpWidget(Container());

verify(platform.dispose(mapId: anyNamed('mapId')));
});
}

// Some test setup classes below...

class _MockStream<T> extends Mock implements Stream<T> {}

typedef _CreationCallback = void Function(int);

// Installs test mocks on the platform
void _setupMock(MockGoogleMapsFlutterPlatform platform) {
// Used to create the view of the map...
when(platform.buildView(any, any, any)).thenAnswer((realInvocation) {
// Call the onPlatformViewCreated callback so the controller gets created.
_CreationCallback onPlatformViewCreatedCb =
realInvocation.positionalArguments[2];
onPlatformViewCreatedCb.call(0);
return Container();
});
// Used to create the Controller
when(platform.onCameraIdle(mapId: anyNamed('mapId')))
.thenAnswer((_) => _MockStream<CameraIdleEvent>());
when(platform.onCameraMove(mapId: anyNamed('mapId')))
.thenAnswer((_) => _MockStream<CameraMoveEvent>());
when(platform.onCameraMoveStarted(mapId: anyNamed('mapId')))
.thenAnswer((_) => _MockStream<CameraMoveStartedEvent>());
when(platform.onCircleTap(mapId: anyNamed('mapId')))
.thenAnswer((_) => _MockStream<CircleTapEvent>());
when(platform.onInfoWindowTap(mapId: anyNamed('mapId')))
.thenAnswer((_) => _MockStream<InfoWindowTapEvent>());
when(platform.onLongPress(mapId: anyNamed('mapId')))
.thenAnswer((_) => _MockStream<MapLongPressEvent>());
when(platform.onMarkerDragEnd(mapId: anyNamed('mapId')))
.thenAnswer((_) => _MockStream<MarkerDragEndEvent>());
when(platform.onMarkerTap(mapId: anyNamed('mapId')))
.thenAnswer((_) => _MockStream<MarkerTapEvent>());
when(platform.onPolygonTap(mapId: anyNamed('mapId')))
.thenAnswer((_) => _MockStream<PolygonTapEvent>());
when(platform.onPolylineTap(mapId: anyNamed('mapId')))
.thenAnswer((_) => _MockStream<PolylineTapEvent>());
when(platform.onTap(mapId: anyNamed('mapId')))
.thenAnswer((_) => _MockStream<MapTapEvent>());
}