Skip to content

Commit

Permalink
[google_maps_flutter] Migrate to NNBD (#3548)
Browse files Browse the repository at this point in the history
Migrates the full plugin to null safety.
  • Loading branch information
stuartmorgan authored Feb 19, 2021
1 parent e8b38f6 commit 849f258
Show file tree
Hide file tree
Showing 32 changed files with 1,003 additions and 890 deletions.
8 changes: 8 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,11 @@
## 2.0.0-nullsafety

* Migrate to null-safety
* BREAKING CHANGE: Passing an unknown map object ID (e.g., MarkerId) to a
method, it will throw an `UnknownMapObjectIDError`. Previously it would
either silently do nothing, or throw an error trying to call a function on
`null`, depneding on the method.

## 1.2.0

* Support custom tiles.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2019, the Chromium project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// @dart=2.9

import 'dart:typed_data';
import 'package:flutter/services.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2019, the Chromium project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// @dart=2.9

import 'dart:async';
import 'dart:io';
Expand Down Expand Up @@ -954,10 +955,8 @@ void main() {
final BitmapDescriptor scaled = await BitmapDescriptor.fromAssetImage(
imageConfiguration, 'red_square.png',
mipmaps: false);
// ignore: invalid_use_of_visible_for_testing_member
expect(mip.toJson()[2], 1);
// ignore: invalid_use_of_visible_for_testing_member
expect(scaled.toJson()[2], 2);
expect((mip.toJson() as List<dynamic>)[2], 1);
expect((scaled.toJson() as List<dynamic>)[2], 2);
});

testWidgets('testTakeSnapshot', (WidgetTester tester) async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class AnimateCamera extends StatefulWidget {
}

class AnimateCameraState extends State<AnimateCamera> {
GoogleMapController mapController;
GoogleMapController? mapController;

void _onMapCreated(GoogleMapController controller) {
mapController = controller;
Expand Down Expand Up @@ -56,7 +56,7 @@ class AnimateCameraState extends State<AnimateCamera> {
children: <Widget>[
TextButton(
onPressed: () {
mapController.animateCamera(
mapController?.animateCamera(
CameraUpdate.newCameraPosition(
const CameraPosition(
bearing: 270.0,
Expand All @@ -71,7 +71,7 @@ class AnimateCameraState extends State<AnimateCamera> {
),
TextButton(
onPressed: () {
mapController.animateCamera(
mapController?.animateCamera(
CameraUpdate.newLatLng(
const LatLng(56.1725505, 10.1850512),
),
Expand All @@ -81,7 +81,7 @@ class AnimateCameraState extends State<AnimateCamera> {
),
TextButton(
onPressed: () {
mapController.animateCamera(
mapController?.animateCamera(
CameraUpdate.newLatLngBounds(
LatLngBounds(
southwest: const LatLng(-38.483935, 113.248673),
Expand All @@ -95,7 +95,7 @@ class AnimateCameraState extends State<AnimateCamera> {
),
TextButton(
onPressed: () {
mapController.animateCamera(
mapController?.animateCamera(
CameraUpdate.newLatLngZoom(
const LatLng(37.4231613, -122.087159),
11.0,
Expand All @@ -106,7 +106,7 @@ class AnimateCameraState extends State<AnimateCamera> {
),
TextButton(
onPressed: () {
mapController.animateCamera(
mapController?.animateCamera(
CameraUpdate.scrollBy(150.0, -225.0),
);
},
Expand All @@ -118,7 +118,7 @@ class AnimateCameraState extends State<AnimateCamera> {
children: <Widget>[
TextButton(
onPressed: () {
mapController.animateCamera(
mapController?.animateCamera(
CameraUpdate.zoomBy(
-0.5,
const Offset(30.0, 20.0),
Expand All @@ -129,31 +129,31 @@ class AnimateCameraState extends State<AnimateCamera> {
),
TextButton(
onPressed: () {
mapController.animateCamera(
mapController?.animateCamera(
CameraUpdate.zoomBy(-0.5),
);
},
child: const Text('zoomBy'),
),
TextButton(
onPressed: () {
mapController.animateCamera(
mapController?.animateCamera(
CameraUpdate.zoomIn(),
);
},
child: const Text('zoomIn'),
),
TextButton(
onPressed: () {
mapController.animateCamera(
mapController?.animateCamera(
CameraUpdate.zoomOut(),
);
},
child: const Text('zoomOut'),
),
TextButton(
onPressed: () {
mapController.animateCamera(
mapController?.animateCamera(
CameraUpdate.zoomTo(16.0),
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class _MapClickBody extends StatefulWidget {
class _MapClickBodyState extends State<_MapClickBody> {
_MapClickBodyState();

GoogleMapController mapController;
LatLng _lastTap;
LatLng _lastLongPress;
GoogleMapController? mapController;
LatLng? _lastTap;
LatLng? _lastLongPress;

@override
Widget build(BuildContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class _MapCoordinatesBody extends StatefulWidget {
class _MapCoordinatesBodyState extends State<_MapCoordinatesBody> {
_MapCoordinatesBodyState();

GoogleMapController mapController;
GoogleMapController? mapController;
LatLngBounds _visibleRegion = LatLngBounds(
southwest: const LatLng(0, 0),
northeast: const LatLng(0, 0),
Expand Down Expand Up @@ -87,7 +87,7 @@ class _MapCoordinatesBodyState extends State<_MapCoordinatesBody> {
child: const Text('Get Visible Region Bounds'),
onPressed: () async {
final LatLngBounds visibleRegion =
await mapController.getVisibleRegion();
await mapController!.getVisibleRegion();
setState(() {
_visibleRegion = visibleRegion;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class MapUiBodyState extends State<MapUiBody> {
bool _myLocationEnabled = true;
bool _myTrafficEnabled = false;
bool _myLocationButtonEnabled = true;
GoogleMapController _controller;
late GoogleMapController _controller;
bool _nightMode = false;

@override
Expand Down Expand Up @@ -249,10 +249,9 @@ class MapUiBodyState extends State<MapUiBody> {
});
}

// Should only be called if _isMapCreated is true.
Widget _nightModeToggler() {
if (!_isMapCreated) {
return null;
}
assert(_isMapCreated);
return TextButton(
child: Text('${_nightMode ? 'disable' : 'enable'} night mode'),
onPressed: () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class MarkerIconsBody extends StatefulWidget {
const LatLng _kMapCenter = LatLng(52.4478, -3.5402);

class MarkerIconsBodyState extends State<MarkerIconsBody> {
GoogleMapController controller;
BitmapDescriptor _markerIcon;
GoogleMapController? controller;
BitmapDescriptor? _markerIcon;

@override
Widget build(BuildContext context) {
Expand All @@ -48,7 +48,7 @@ class MarkerIconsBodyState extends State<MarkerIconsBody> {
target: _kMapCenter,
zoom: 7.0,
),
markers: _createMarker(),
markers: <Marker>{_createMarker()},
onMapCreated: _onMapCreated,
),
),
Expand All @@ -57,17 +57,19 @@ class MarkerIconsBodyState extends State<MarkerIconsBody> {
);
}

Set<Marker> _createMarker() {
// TODO(iskakaushik): Remove this when collection literals makes it to stable.
// https://github.com/flutter/flutter/issues/28312
// ignore: prefer_collection_literals
return <Marker>[
Marker(
Marker _createMarker() {
if (_markerIcon != null) {
return Marker(
markerId: MarkerId("marker_1"),
position: _kMapCenter,
icon: _markerIcon,
),
].toSet();
icon: _markerIcon!,
);
} else {
return Marker(
markerId: MarkerId("marker_1"),
position: _kMapCenter,
);
}
}

Future<void> _createMarkerImageFromAsset(BuildContext context) async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class MoveCamera extends StatefulWidget {
}

class MoveCameraState extends State<MoveCamera> {
GoogleMapController mapController;
GoogleMapController? mapController;

void _onMapCreated(GoogleMapController controller) {
mapController = controller;
Expand Down Expand Up @@ -55,7 +55,7 @@ class MoveCameraState extends State<MoveCamera> {
children: <Widget>[
TextButton(
onPressed: () {
mapController.moveCamera(
mapController?.moveCamera(
CameraUpdate.newCameraPosition(
const CameraPosition(
bearing: 270.0,
Expand All @@ -70,7 +70,7 @@ class MoveCameraState extends State<MoveCamera> {
),
TextButton(
onPressed: () {
mapController.moveCamera(
mapController?.moveCamera(
CameraUpdate.newLatLng(
const LatLng(56.1725505, 10.1850512),
),
Expand All @@ -80,7 +80,7 @@ class MoveCameraState extends State<MoveCamera> {
),
TextButton(
onPressed: () {
mapController.moveCamera(
mapController?.moveCamera(
CameraUpdate.newLatLngBounds(
LatLngBounds(
southwest: const LatLng(-38.483935, 113.248673),
Expand All @@ -94,7 +94,7 @@ class MoveCameraState extends State<MoveCamera> {
),
TextButton(
onPressed: () {
mapController.moveCamera(
mapController?.moveCamera(
CameraUpdate.newLatLngZoom(
const LatLng(37.4231613, -122.087159),
11.0,
Expand All @@ -105,7 +105,7 @@ class MoveCameraState extends State<MoveCamera> {
),
TextButton(
onPressed: () {
mapController.moveCamera(
mapController?.moveCamera(
CameraUpdate.scrollBy(150.0, -225.0),
);
},
Expand All @@ -117,7 +117,7 @@ class MoveCameraState extends State<MoveCamera> {
children: <Widget>[
TextButton(
onPressed: () {
mapController.moveCamera(
mapController?.moveCamera(
CameraUpdate.zoomBy(
-0.5,
const Offset(30.0, 20.0),
Expand All @@ -128,31 +128,31 @@ class MoveCameraState extends State<MoveCamera> {
),
TextButton(
onPressed: () {
mapController.moveCamera(
mapController?.moveCamera(
CameraUpdate.zoomBy(-0.5),
);
},
child: const Text('zoomBy'),
),
TextButton(
onPressed: () {
mapController.moveCamera(
mapController?.moveCamera(
CameraUpdate.zoomIn(),
);
},
child: const Text('zoomIn'),
),
TextButton(
onPressed: () {
mapController.moveCamera(
mapController?.moveCamera(
CameraUpdate.zoomOut(),
);
},
child: const Text('zoomOut'),
),
TextButton(
onPressed: () {
mapController.moveCamera(
mapController?.moveCamera(
CameraUpdate.zoomTo(16.0),
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class MarkerIconsBody extends StatefulWidget {
const LatLng _kMapCenter = LatLng(52.4478, -3.5402);

class MarkerIconsBodyState extends State<MarkerIconsBody> {
GoogleMapController controller;
GoogleMapController? controller;

EdgeInsets _padding = const EdgeInsets.all(0);

Expand Down Expand Up @@ -152,10 +152,10 @@ class MarkerIconsBodyState extends State<MarkerIconsBody> {
onPressed: () {
setState(() {
_padding = EdgeInsets.fromLTRB(
double.tryParse(_leftController.value?.text) ?? 0,
double.tryParse(_topController.value?.text) ?? 0,
double.tryParse(_rightController.value?.text) ?? 0,
double.tryParse(_bottomController.value?.text) ?? 0);
double.tryParse(_leftController.value.text) ?? 0,
double.tryParse(_topController.value.text) ?? 0,
double.tryParse(_rightController.value.text) ?? 0,
double.tryParse(_bottomController.value.text) ?? 0);
});
},
),
Expand Down
Loading

0 comments on commit 849f258

Please sign in to comment.