Skip to content

chore: handle platform exceptions #371

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
34 changes: 22 additions & 12 deletions example/lib/pages/camera.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,23 @@ class _CameraPageState extends ExamplePageState<CameraPage> {
bool _showCameraUpdates = false;
String _latestCameraUpdate = '';
bool _isFollowingLocation = false;
bool _ready = false;

// ignore: use_setters_to_change_properties
Future<void> _onViewCreated(GoogleNavigationViewController controller) async {
_navigationViewController = controller;
calculateFocusCenter();
_minZoomLevel = await _navigationViewController.getMinZoomPreference();
_maxZoomLevel = await _navigationViewController.getMaxZoomPreference();

if (mounted) {
setState(() {});
try {
_navigationViewController = controller;
calculateFocusCenter();
_minZoomLevel = await _navigationViewController.getMinZoomPreference();
_maxZoomLevel = await _navigationViewController.getMaxZoomPreference();
if (mounted) {
setState(() {
_ready = true;
});
}
} on ViewNotFoundException catch (_) {
// View not found exception is thrown if view is disposed before async
// method is handled on native side.
}
}

Expand Down Expand Up @@ -148,7 +155,8 @@ class _CameraPageState extends ExamplePageState<CameraPage> {
_onCameraStartedFollowingLocation,
onCameraStoppedFollowingLocation:
_onCameraStoppedFollowingLocation),
getOverlayOptionsButton(context, onPressed: () => toggleOverlay()),
getOverlayOptionsButton(context,
onPressed: _ready ? () => toggleOverlay() : null),
if (_showCameraUpdates)
Container(
width: 180,
Expand Down Expand Up @@ -246,8 +254,9 @@ class _CameraPageState extends ExamplePageState<CameraPage> {
setState(() {
_minZoomLevel = newMinZoomLevel;
});
} catch (e) {
showMessage(e.toString());
} on MinZoomRangeException catch (e) {
debugPrint(e.toString());
showMessage('Cannot set min zoom');
}
}

Expand All @@ -257,8 +266,9 @@ class _CameraPageState extends ExamplePageState<CameraPage> {
setState(() {
_maxZoomLevel = newMaxZoomLevel;
});
} catch (e) {
showMessage(e.toString());
} on MaxZoomRangeException catch (e) {
debugPrint(e.toString());
showMessage('Cannot set max zoom');
}
}

Expand Down
20 changes: 17 additions & 3 deletions example/lib/pages/circles.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,16 @@ class _CirclesPageState extends ExamplePageState<CirclesPage> {
Future<void> _updateSelectedCircleWithOptions(CircleOptions options) async {
final Circle newCircle = _selectedCircle!.copyWith(options: options);

final List<Circle?> circles =
await _navigationViewController.updateCircles(<Circle>[newCircle]);
final List<Circle?> circles;
try {
circles =
await _navigationViewController.updateCircles(<Circle>[newCircle]);
} on CircleNotFoundException catch (e) {
debugPrint(e.toString());
showMessage('Circle not found');
return;
}

final Circle? circle = circles.firstOrNull;
if (circle != null) {
setState(() {
Expand All @@ -102,7 +110,13 @@ class _CirclesPageState extends ExamplePageState<CirclesPage> {
}

Future<void> _removeCircle() async {
await _navigationViewController.removeCircles(<Circle>[_selectedCircle!]);
try {
await _navigationViewController.removeCircles(<Circle>[_selectedCircle!]);
} on CircleNotFoundException catch (e) {
debugPrint(e.toString());
showMessage('Circle not found');
return;
}

setState(() {
_circles = _circles
Expand Down
41 changes: 26 additions & 15 deletions example/lib/pages/map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ class BasicMapPage extends ExamplePage {

class _MapPageState extends ExamplePageState<BasicMapPage> {
late final GoogleMapViewController _mapViewController;
late bool isMyLocationEnabled = false;
late bool isMyLocationButtonEnabled = true;
late bool consumeMyLocationButtonClickEvent = false;
late bool isZoomGesturesEnabled = true;
late bool isZoomControlsEnabled = true;
late bool isCompassEnabled = true;
late bool isRotateGesturesEnabled = true;
late bool isScrollGesturesEnabled = true;
late bool isScrollGesturesEnabledDuringRotateOrZoom = true;
late bool isTiltGesturesEnabled = true;
late bool isTrafficEnabled = false;
late MapType mapType = MapType.normal;
bool isMyLocationEnabled = false;
bool isMyLocationButtonEnabled = true;
bool consumeMyLocationButtonClickEvent = false;
bool isZoomGesturesEnabled = true;
bool isZoomControlsEnabled = true;
bool isCompassEnabled = true;
bool isRotateGesturesEnabled = true;
bool isScrollGesturesEnabled = true;
bool isScrollGesturesEnabledDuringRotateOrZoom = true;
bool isTiltGesturesEnabled = true;
bool isTrafficEnabled = false;
MapType mapType = MapType.normal;

Future<void> setMapType(MapType type) async {
mapType = type;
Expand All @@ -65,13 +65,24 @@ class _MapPageState extends ExamplePageState<BasicMapPage> {
Future<void> setMapStyleNight() async {
final String jsonString =
await rootBundle.loadString('assets/night_style.json');
await _mapViewController.setMapStyle(jsonString);

try {
await _mapViewController.setMapStyle(jsonString);
} on MapStyleException catch (e) {
debugPrint(e.toString());
_showMessage('Unable to set map style');
}
}

Future<void> setMapStyleSepia() async {
final String jsonString =
await rootBundle.loadString('assets/sepia_style.json');
await _mapViewController.setMapStyle(jsonString);
try {
await _mapViewController.setMapStyle(jsonString);
} on MapStyleException catch (e) {
debugPrint(e.toString());
_showMessage('Unable to set map style');
}
}

// ignore: use_setters_to_change_properties
Expand Down Expand Up @@ -172,7 +183,7 @@ class _MapPageState extends ExamplePageState<BasicMapPage> {
),
),
getOverlayOptionsButton(context,
onPressed: () => toggleOverlay())
onPressed: () => toggleOverlay()),
],
));
}
Expand Down
33 changes: 28 additions & 5 deletions example/lib/pages/markers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,14 @@ class _MarkersPageState extends ExamplePageState<MarkersPage> {
}

Future<void> _removeMarker() async {
await _navigationViewController.removeMarkers(<Marker>[_selectedMarker!]);
try {
await _navigationViewController.removeMarkers(<Marker>[_selectedMarker!]);
} on MarkerNotFoundException catch (e) {
debugPrint(e.toString());
showMessage('Marker not found');
return;
}

setState(() {
_markers.remove(_selectedMarker);
_selectedMarker = null;
Expand All @@ -86,8 +93,16 @@ class _MarkersPageState extends ExamplePageState<MarkersPage> {
Future<void> _updateSelectedMarkerWithOptions(MarkerOptions options) async {
final Marker newMarker = _selectedMarker!.copyWith(options: options);

final List<Marker?> markers =
await _navigationViewController.updateMarkers(<Marker>[newMarker]);
final List<Marker?> markers;
try {
markers =
await _navigationViewController.updateMarkers(<Marker>[newMarker]);
} on MarkerNotFoundException catch (e) {
debugPrint(e.toString());
showMessage('Marker not found');
return;
}

final Marker? marker = markers.firstOrNull;
if (marker != null) {
setState(() {
Expand Down Expand Up @@ -153,8 +168,16 @@ class _MarkersPageState extends ExamplePageState<MarkersPage> {
await assetImage.obtainKey(configuration);
final double imagePixelRatio = assetBundleImageKey.scale;
final ByteData imageBytes = await rootBundle.load(assetBundleImageKey.name);
_registeredCustomIcon = await registerBitmapImage(
bitmap: imageBytes, imagePixelRatio: imagePixelRatio);

try {
_registeredCustomIcon = await registerBitmapImage(
bitmap: imageBytes, imagePixelRatio: imagePixelRatio);
} on ImageDecodingFailedException catch (e) {
debugPrint(e.toString());
showMessage('Failed to decode image');
return ImageDescriptor.defaultImage;
}

return _registeredCustomIcon!;
}

Expand Down
8 changes: 7 additions & 1 deletion example/lib/pages/multiple_views.dart
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,13 @@ class _MultiplexState extends ExamplePageState<MultipleMapViewsPage> {
setState(() {
_navigationController = controller;
});
await controller.setMyLocationEnabled(true);

try {
await controller.setMyLocationEnabled(true);
} on ViewNotFoundException catch (_) {
// View not found exception is thrown if view is disposed before async
// method is handled on native side.
}
}

Future<void> _moveCameras() async {
Expand Down
92 changes: 69 additions & 23 deletions example/lib/pages/navigation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,13 @@ class _NavigationPageState extends ExamplePageState<NavigationPage> {
setState(() {
_navigationViewController = controller;
});
await controller.setMyLocationEnabled(true);

try {
await controller.setMyLocationEnabled(true);
} on ViewNotFoundException catch (_) {
// View not found exception is thrown if view is disposed before async
// method is handled on native side.
}

if (_guidanceRunning) {
// Guidance is running, enable navigation UI.
Expand Down Expand Up @@ -611,8 +617,17 @@ class _NavigationPageState extends ExamplePageState<NavigationPage> {
// Update existing marker.
final Marker updatedWaypointMarker =
_newWaypointMarker!.copyWith(options: markerOptions);
final List<Marker?> updatedMarkers = await _navigationViewController!
.updateMarkers(<Marker>[updatedWaypointMarker]);

final List<Marker?> updatedMarkers;
try {
updatedMarkers = await _navigationViewController!
.updateMarkers(<Marker>[updatedWaypointMarker]);
} on MarkerNotFoundException catch (e) {
debugPrint(e.toString());
showMessage('Marker not found');
return;
}

if (updatedMarkers.first != null) {
_newWaypointMarker = updatedMarkers.first;
} else {
Expand All @@ -623,24 +638,38 @@ class _NavigationPageState extends ExamplePageState<NavigationPage> {
}

Future<void> _removeNewWaypointMarker() async {
if (_newWaypointMarker != null) {
if (_newWaypointMarker == null) return;

try {
await _navigationViewController!
.removeMarkers(<Marker>[_newWaypointMarker!]);
_newWaypointMarker = null;
setState(() {});
} on MarkerNotFoundException catch (e) {
debugPrint(e.toString());
showMessage('Marker not found');
return;
}

_newWaypointMarker = null;
setState(() {});
}

Future<void> _removeDestinationWaypointMarkers() async {
if (_destinationWaypointMarkers.isNotEmpty) {
if (_destinationWaypointMarkers.isEmpty) return;

try {
await _navigationViewController!
.removeMarkers(_destinationWaypointMarkers);
_destinationWaypointMarkers.clear();

// Unregister custom marker images
await clearRegisteredImages();
setState(() {});
} on MarkerNotFoundException catch (e) {
debugPrint(e.toString());
showMessage('Marker not found');
return;
}

_destinationWaypointMarkers.clear();

// Unregister custom marker images
await clearRegisteredImages();
setState(() {});
}

Future<void> _onMapClicked(LatLng location) async {
Expand Down Expand Up @@ -690,16 +719,25 @@ class _NavigationPageState extends ExamplePageState<NavigationPage> {
final ImageDescriptor waypointMarkerImage =
await registerWaypointMarkerImage(
index, MediaQuery.of(context).devicePixelRatio);
final List<Marker?> destinationMarkers =
await _navigationViewController!.updateMarkers(<Marker>[
_newWaypointMarker!.copyWith(
options: _newWaypointMarker!.options.copyWith(
infoWindow: InfoWindow(title: title),
anchor: const MarkerAnchor(u: 0.5, v: 1.2),
icon: waypointMarkerImage,
),
)
]);

final List<Marker?> destinationMarkers;
try {
destinationMarkers =
await _navigationViewController!.updateMarkers(<Marker>[
_newWaypointMarker!.copyWith(
options: _newWaypointMarker!.options.copyWith(
infoWindow: InfoWindow(title: title),
anchor: const MarkerAnchor(u: 0.5, v: 1.2),
icon: waypointMarkerImage,
),
)
]);
} on MarkerNotFoundException catch (e) {
debugPrint(e.toString());
showMessage('Marker not found');
return;
}

_destinationWaypointMarkers.add(destinationMarkers.first!);
_newWaypointMarker = null;
}
Expand All @@ -723,7 +761,15 @@ class _NavigationPageState extends ExamplePageState<NavigationPage> {
// Remove the first destination marker from the list.
if (_destinationWaypointMarkers.isNotEmpty) {
final Marker markerToRemove = _destinationWaypointMarkers.first;
await _navigationViewController!.removeMarkers(<Marker>[markerToRemove]);

try {
await _navigationViewController!
.removeMarkers(<Marker>[markerToRemove]);
} on MarkerNotFoundException catch (e) {
debugPrint(e.toString());
showMessage('Marker not found');
return;
}

// Unregister custom marker image.
await unregisterImage(markerToRemove.options.icon);
Expand Down
Loading
Loading