Skip to content

Commit d7a856f

Browse files
ditmanEgor
authored and
Egor
committed
[google_maps_flutter] Call platform.dispose from widget. (flutter#2909)
1 parent f4af22c commit d7a856f

File tree

5 files changed

+77
-3
lines changed

5 files changed

+77
-3
lines changed

packages/google_maps_flutter/google_maps_flutter/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.5.30
2+
3+
* Add a `dispose` method to the controller to let the native side know that we're done with said controller.
4+
* Call `controller.dispose()` from the `dispose` method of the `GoogleMap` widget.
5+
16
## 0.5.29+1
27

38
* (ios) Pin dependency on GoogleMaps pod to `< 3.10`, to address https://github.com/flutter/flutter/issues/63447

packages/google_maps_flutter/google_maps_flutter/lib/src/controller.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,9 @@ class GoogleMapController {
258258
Future<Uint8List> takeSnapshot() {
259259
return _googleMapsFlutterPlatform.takeSnapshot(mapId: mapId);
260260
}
261+
262+
/// Disposes of the platform resources
263+
void dispose() {
264+
_googleMapsFlutterPlatform.dispose(mapId: mapId);
265+
}
261266
}

packages/google_maps_flutter/google_maps_flutter/lib/src/google_map.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,13 @@ class _GoogleMapState extends State<GoogleMap> {
251251
_circles = keyByCircleId(widget.circles);
252252
}
253253

254+
@override
255+
void dispose() async {
256+
super.dispose();
257+
GoogleMapController controller = await _controller.future;
258+
controller.dispose();
259+
}
260+
254261
@override
255262
void didUpdateWidget(GoogleMap oldWidget) {
256263
super.didUpdateWidget(oldWidget);

packages/google_maps_flutter/google_maps_flutter/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
name: google_maps_flutter
22
description: A Flutter plugin for integrating Google Maps in iOS and Android applications.
33
homepage: https://github.com/flutter/plugins/tree/master/packages/google_maps_flutter/google_maps_flutter
4-
version: 0.5.29+1
4+
version: 0.5.30
55

66
dependencies:
77
flutter:
88
sdk: flutter
99
flutter_plugin_android_lifecycle: ^1.0.0
10-
google_maps_flutter_platform_interface: ^1.0.1
10+
google_maps_flutter_platform_interface: ^1.0.4
1111

1212
dev_dependencies:
1313
flutter_test:

packages/google_maps_flutter/google_maps_flutter/test/map_creation_test.dart

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ void main() {
2020
setUp(() {
2121
// Use a mock platform so we never need to hit the MethodChannel code.
2222
GoogleMapsFlutterPlatform.instance = platform;
23-
when(platform.buildView(any, any, any)).thenReturn(Container());
23+
resetMockitoState();
24+
_setupMock(platform);
2425
});
2526

2627
testWidgets('_webOnlyMapCreationId increments with each GoogleMap widget', (
@@ -61,4 +62,60 @@ void main() {
6162
),
6263
]);
6364
});
65+
66+
testWidgets('Calls platform.dispose when GoogleMap is disposed of', (
67+
WidgetTester tester,
68+
) async {
69+
await tester.pumpWidget(GoogleMap(
70+
initialCameraPosition: CameraPosition(
71+
target: LatLng(43.3608, -5.8702),
72+
),
73+
));
74+
75+
// Now dispose of the map...
76+
await tester.pumpWidget(Container());
77+
78+
verify(platform.dispose(mapId: anyNamed('mapId')));
79+
});
80+
}
81+
82+
// Some test setup classes below...
83+
84+
class _MockStream<T> extends Mock implements Stream<T> {}
85+
86+
typedef _CreationCallback = void Function(int);
87+
88+
// Installs test mocks on the platform
89+
void _setupMock(MockGoogleMapsFlutterPlatform platform) {
90+
// Used to create the view of the map...
91+
when(platform.buildView(any, any, any)).thenAnswer((realInvocation) {
92+
// Call the onPlatformViewCreated callback so the controller gets created.
93+
_CreationCallback onPlatformViewCreatedCb =
94+
realInvocation.positionalArguments[2];
95+
onPlatformViewCreatedCb.call(0);
96+
return Container();
97+
});
98+
// Used to create the Controller
99+
when(platform.onCameraIdle(mapId: anyNamed('mapId')))
100+
.thenAnswer((_) => _MockStream<CameraIdleEvent>());
101+
when(platform.onCameraMove(mapId: anyNamed('mapId')))
102+
.thenAnswer((_) => _MockStream<CameraMoveEvent>());
103+
when(platform.onCameraMoveStarted(mapId: anyNamed('mapId')))
104+
.thenAnswer((_) => _MockStream<CameraMoveStartedEvent>());
105+
when(platform.onCircleTap(mapId: anyNamed('mapId')))
106+
.thenAnswer((_) => _MockStream<CircleTapEvent>());
107+
when(platform.onInfoWindowTap(mapId: anyNamed('mapId')))
108+
.thenAnswer((_) => _MockStream<InfoWindowTapEvent>());
109+
when(platform.onLongPress(mapId: anyNamed('mapId')))
110+
.thenAnswer((_) => _MockStream<MapLongPressEvent>());
111+
when(platform.onMarkerDragEnd(mapId: anyNamed('mapId')))
112+
.thenAnswer((_) => _MockStream<MarkerDragEndEvent>());
113+
when(platform.onMarkerTap(mapId: anyNamed('mapId')))
114+
.thenAnswer((_) => _MockStream<MarkerTapEvent>());
115+
when(platform.onPolygonTap(mapId: anyNamed('mapId')))
116+
.thenAnswer((_) => _MockStream<PolygonTapEvent>());
117+
when(platform.onPolylineTap(mapId: anyNamed('mapId')))
118+
.thenAnswer((_) => _MockStream<PolylineTapEvent>());
119+
when(platform.onTap(mapId: anyNamed('mapId')))
120+
.thenAnswer((_) => _MockStream<MapTapEvent>());
64121
}

0 commit comments

Comments
 (0)