Skip to content

Commit 849f258

Browse files
[google_maps_flutter] Migrate to NNBD (#3548)
Migrates the full plugin to null safety.
1 parent e8b38f6 commit 849f258

32 files changed

+1003
-890
lines changed

packages/google_maps_flutter/google_maps_flutter/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## 2.0.0-nullsafety
2+
3+
* Migrate to null-safety
4+
* BREAKING CHANGE: Passing an unknown map object ID (e.g., MarkerId) to a
5+
method, it will throw an `UnknownMapObjectIDError`. Previously it would
6+
either silently do nothing, or throw an error trying to call a function on
7+
`null`, depneding on the method.
8+
19
## 1.2.0
210

311
* Support custom tiles.

packages/google_maps_flutter/google_maps_flutter/example/integration_test/google_map_inspector.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2019, the Chromium project authors. Please see the AUTHORS file
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
4+
// @dart=2.9
45

56
import 'dart:typed_data';
67
import 'package:flutter/services.dart';

packages/google_maps_flutter/google_maps_flutter/example/integration_test/google_maps_test.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2019, the Chromium project authors. Please see the AUTHORS file
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
4+
// @dart=2.9
45

56
import 'dart:async';
67
import 'dart:io';
@@ -954,10 +955,8 @@ void main() {
954955
final BitmapDescriptor scaled = await BitmapDescriptor.fromAssetImage(
955956
imageConfiguration, 'red_square.png',
956957
mipmaps: false);
957-
// ignore: invalid_use_of_visible_for_testing_member
958-
expect(mip.toJson()[2], 1);
959-
// ignore: invalid_use_of_visible_for_testing_member
960-
expect(scaled.toJson()[2], 2);
958+
expect((mip.toJson() as List<dynamic>)[2], 1);
959+
expect((scaled.toJson() as List<dynamic>)[2], 2);
961960
});
962961

963962
testWidgets('testTakeSnapshot', (WidgetTester tester) async {

packages/google_maps_flutter/google_maps_flutter/example/lib/animate_camera.dart

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class AnimateCamera extends StatefulWidget {
2626
}
2727

2828
class AnimateCameraState extends State<AnimateCamera> {
29-
GoogleMapController mapController;
29+
GoogleMapController? mapController;
3030

3131
void _onMapCreated(GoogleMapController controller) {
3232
mapController = controller;
@@ -56,7 +56,7 @@ class AnimateCameraState extends State<AnimateCamera> {
5656
children: <Widget>[
5757
TextButton(
5858
onPressed: () {
59-
mapController.animateCamera(
59+
mapController?.animateCamera(
6060
CameraUpdate.newCameraPosition(
6161
const CameraPosition(
6262
bearing: 270.0,
@@ -71,7 +71,7 @@ class AnimateCameraState extends State<AnimateCamera> {
7171
),
7272
TextButton(
7373
onPressed: () {
74-
mapController.animateCamera(
74+
mapController?.animateCamera(
7575
CameraUpdate.newLatLng(
7676
const LatLng(56.1725505, 10.1850512),
7777
),
@@ -81,7 +81,7 @@ class AnimateCameraState extends State<AnimateCamera> {
8181
),
8282
TextButton(
8383
onPressed: () {
84-
mapController.animateCamera(
84+
mapController?.animateCamera(
8585
CameraUpdate.newLatLngBounds(
8686
LatLngBounds(
8787
southwest: const LatLng(-38.483935, 113.248673),
@@ -95,7 +95,7 @@ class AnimateCameraState extends State<AnimateCamera> {
9595
),
9696
TextButton(
9797
onPressed: () {
98-
mapController.animateCamera(
98+
mapController?.animateCamera(
9999
CameraUpdate.newLatLngZoom(
100100
const LatLng(37.4231613, -122.087159),
101101
11.0,
@@ -106,7 +106,7 @@ class AnimateCameraState extends State<AnimateCamera> {
106106
),
107107
TextButton(
108108
onPressed: () {
109-
mapController.animateCamera(
109+
mapController?.animateCamera(
110110
CameraUpdate.scrollBy(150.0, -225.0),
111111
);
112112
},
@@ -118,7 +118,7 @@ class AnimateCameraState extends State<AnimateCamera> {
118118
children: <Widget>[
119119
TextButton(
120120
onPressed: () {
121-
mapController.animateCamera(
121+
mapController?.animateCamera(
122122
CameraUpdate.zoomBy(
123123
-0.5,
124124
const Offset(30.0, 20.0),
@@ -129,31 +129,31 @@ class AnimateCameraState extends State<AnimateCamera> {
129129
),
130130
TextButton(
131131
onPressed: () {
132-
mapController.animateCamera(
132+
mapController?.animateCamera(
133133
CameraUpdate.zoomBy(-0.5),
134134
);
135135
},
136136
child: const Text('zoomBy'),
137137
),
138138
TextButton(
139139
onPressed: () {
140-
mapController.animateCamera(
140+
mapController?.animateCamera(
141141
CameraUpdate.zoomIn(),
142142
);
143143
},
144144
child: const Text('zoomIn'),
145145
),
146146
TextButton(
147147
onPressed: () {
148-
mapController.animateCamera(
148+
mapController?.animateCamera(
149149
CameraUpdate.zoomOut(),
150150
);
151151
},
152152
child: const Text('zoomOut'),
153153
),
154154
TextButton(
155155
onPressed: () {
156-
mapController.animateCamera(
156+
mapController?.animateCamera(
157157
CameraUpdate.zoomTo(16.0),
158158
);
159159
},

packages/google_maps_flutter/google_maps_flutter/example/lib/map_click.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ class _MapClickBody extends StatefulWidget {
3131
class _MapClickBodyState extends State<_MapClickBody> {
3232
_MapClickBodyState();
3333

34-
GoogleMapController mapController;
35-
LatLng _lastTap;
36-
LatLng _lastLongPress;
34+
GoogleMapController? mapController;
35+
LatLng? _lastTap;
36+
LatLng? _lastLongPress;
3737

3838
@override
3939
Widget build(BuildContext context) {

packages/google_maps_flutter/google_maps_flutter/example/lib/map_coordinates.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class _MapCoordinatesBody extends StatefulWidget {
3131
class _MapCoordinatesBodyState extends State<_MapCoordinatesBody> {
3232
_MapCoordinatesBodyState();
3333

34-
GoogleMapController mapController;
34+
GoogleMapController? mapController;
3535
LatLngBounds _visibleRegion = LatLngBounds(
3636
southwest: const LatLng(0, 0),
3737
northeast: const LatLng(0, 0),
@@ -87,7 +87,7 @@ class _MapCoordinatesBodyState extends State<_MapCoordinatesBody> {
8787
child: const Text('Get Visible Region Bounds'),
8888
onPressed: () async {
8989
final LatLngBounds visibleRegion =
90-
await mapController.getVisibleRegion();
90+
await mapController!.getVisibleRegion();
9191
setState(() {
9292
_visibleRegion = visibleRegion;
9393
});

packages/google_maps_flutter/google_maps_flutter/example/lib/map_ui.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class MapUiBodyState extends State<MapUiBody> {
5656
bool _myLocationEnabled = true;
5757
bool _myTrafficEnabled = false;
5858
bool _myLocationButtonEnabled = true;
59-
GoogleMapController _controller;
59+
late GoogleMapController _controller;
6060
bool _nightMode = false;
6161

6262
@override
@@ -249,10 +249,9 @@ class MapUiBodyState extends State<MapUiBody> {
249249
});
250250
}
251251

252+
// Should only be called if _isMapCreated is true.
252253
Widget _nightModeToggler() {
253-
if (!_isMapCreated) {
254-
return null;
255-
}
254+
assert(_isMapCreated);
256255
return TextButton(
257256
child: Text('${_nightMode ? 'disable' : 'enable'} night mode'),
258257
onPressed: () {

packages/google_maps_flutter/google_maps_flutter/example/lib/marker_icons.dart

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class MarkerIconsBody extends StatefulWidget {
2929
const LatLng _kMapCenter = LatLng(52.4478, -3.5402);
3030

3131
class MarkerIconsBodyState extends State<MarkerIconsBody> {
32-
GoogleMapController controller;
33-
BitmapDescriptor _markerIcon;
32+
GoogleMapController? controller;
33+
BitmapDescriptor? _markerIcon;
3434

3535
@override
3636
Widget build(BuildContext context) {
@@ -48,7 +48,7 @@ class MarkerIconsBodyState extends State<MarkerIconsBody> {
4848
target: _kMapCenter,
4949
zoom: 7.0,
5050
),
51-
markers: _createMarker(),
51+
markers: <Marker>{_createMarker()},
5252
onMapCreated: _onMapCreated,
5353
),
5454
),
@@ -57,17 +57,19 @@ class MarkerIconsBodyState extends State<MarkerIconsBody> {
5757
);
5858
}
5959

60-
Set<Marker> _createMarker() {
61-
// TODO(iskakaushik): Remove this when collection literals makes it to stable.
62-
// https://github.com/flutter/flutter/issues/28312
63-
// ignore: prefer_collection_literals
64-
return <Marker>[
65-
Marker(
60+
Marker _createMarker() {
61+
if (_markerIcon != null) {
62+
return Marker(
6663
markerId: MarkerId("marker_1"),
6764
position: _kMapCenter,
68-
icon: _markerIcon,
69-
),
70-
].toSet();
65+
icon: _markerIcon!,
66+
);
67+
} else {
68+
return Marker(
69+
markerId: MarkerId("marker_1"),
70+
position: _kMapCenter,
71+
);
72+
}
7173
}
7274

7375
Future<void> _createMarkerImageFromAsset(BuildContext context) async {

packages/google_maps_flutter/google_maps_flutter/example/lib/move_camera.dart

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class MoveCamera extends StatefulWidget {
2525
}
2626

2727
class MoveCameraState extends State<MoveCamera> {
28-
GoogleMapController mapController;
28+
GoogleMapController? mapController;
2929

3030
void _onMapCreated(GoogleMapController controller) {
3131
mapController = controller;
@@ -55,7 +55,7 @@ class MoveCameraState extends State<MoveCamera> {
5555
children: <Widget>[
5656
TextButton(
5757
onPressed: () {
58-
mapController.moveCamera(
58+
mapController?.moveCamera(
5959
CameraUpdate.newCameraPosition(
6060
const CameraPosition(
6161
bearing: 270.0,
@@ -70,7 +70,7 @@ class MoveCameraState extends State<MoveCamera> {
7070
),
7171
TextButton(
7272
onPressed: () {
73-
mapController.moveCamera(
73+
mapController?.moveCamera(
7474
CameraUpdate.newLatLng(
7575
const LatLng(56.1725505, 10.1850512),
7676
),
@@ -80,7 +80,7 @@ class MoveCameraState extends State<MoveCamera> {
8080
),
8181
TextButton(
8282
onPressed: () {
83-
mapController.moveCamera(
83+
mapController?.moveCamera(
8484
CameraUpdate.newLatLngBounds(
8585
LatLngBounds(
8686
southwest: const LatLng(-38.483935, 113.248673),
@@ -94,7 +94,7 @@ class MoveCameraState extends State<MoveCamera> {
9494
),
9595
TextButton(
9696
onPressed: () {
97-
mapController.moveCamera(
97+
mapController?.moveCamera(
9898
CameraUpdate.newLatLngZoom(
9999
const LatLng(37.4231613, -122.087159),
100100
11.0,
@@ -105,7 +105,7 @@ class MoveCameraState extends State<MoveCamera> {
105105
),
106106
TextButton(
107107
onPressed: () {
108-
mapController.moveCamera(
108+
mapController?.moveCamera(
109109
CameraUpdate.scrollBy(150.0, -225.0),
110110
);
111111
},
@@ -117,7 +117,7 @@ class MoveCameraState extends State<MoveCamera> {
117117
children: <Widget>[
118118
TextButton(
119119
onPressed: () {
120-
mapController.moveCamera(
120+
mapController?.moveCamera(
121121
CameraUpdate.zoomBy(
122122
-0.5,
123123
const Offset(30.0, 20.0),
@@ -128,31 +128,31 @@ class MoveCameraState extends State<MoveCamera> {
128128
),
129129
TextButton(
130130
onPressed: () {
131-
mapController.moveCamera(
131+
mapController?.moveCamera(
132132
CameraUpdate.zoomBy(-0.5),
133133
);
134134
},
135135
child: const Text('zoomBy'),
136136
),
137137
TextButton(
138138
onPressed: () {
139-
mapController.moveCamera(
139+
mapController?.moveCamera(
140140
CameraUpdate.zoomIn(),
141141
);
142142
},
143143
child: const Text('zoomIn'),
144144
),
145145
TextButton(
146146
onPressed: () {
147-
mapController.moveCamera(
147+
mapController?.moveCamera(
148148
CameraUpdate.zoomOut(),
149149
);
150150
},
151151
child: const Text('zoomOut'),
152152
),
153153
TextButton(
154154
onPressed: () {
155-
mapController.moveCamera(
155+
mapController?.moveCamera(
156156
CameraUpdate.zoomTo(16.0),
157157
);
158158
},

packages/google_maps_flutter/google_maps_flutter/example/lib/padding.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class MarkerIconsBody extends StatefulWidget {
2727
const LatLng _kMapCenter = LatLng(52.4478, -3.5402);
2828

2929
class MarkerIconsBodyState extends State<MarkerIconsBody> {
30-
GoogleMapController controller;
30+
GoogleMapController? controller;
3131

3232
EdgeInsets _padding = const EdgeInsets.all(0);
3333

@@ -152,10 +152,10 @@ class MarkerIconsBodyState extends State<MarkerIconsBody> {
152152
onPressed: () {
153153
setState(() {
154154
_padding = EdgeInsets.fromLTRB(
155-
double.tryParse(_leftController.value?.text) ?? 0,
156-
double.tryParse(_topController.value?.text) ?? 0,
157-
double.tryParse(_rightController.value?.text) ?? 0,
158-
double.tryParse(_bottomController.value?.text) ?? 0);
155+
double.tryParse(_leftController.value.text) ?? 0,
156+
double.tryParse(_topController.value.text) ?? 0,
157+
double.tryParse(_rightController.value.text) ?? 0,
158+
double.tryParse(_bottomController.value.text) ?? 0);
159159
});
160160
},
161161
),

0 commit comments

Comments
 (0)