diff --git a/lib/src/directions_repository.dart b/lib/src/directions_repository.dart index b6b6bc8..aaffa2d 100644 --- a/lib/src/directions_repository.dart +++ b/lib/src/directions_repository.dart @@ -6,7 +6,7 @@ class DirectionsRepository { static const String _baseUrl = '/maps/api/directions/json'; /// get the route between the two coordinates - Future getDirections({ + Future getDirections({ required LatLng origin, required LatLng destination, required TravelMode mode, @@ -32,7 +32,11 @@ class DirectionsRepository { }, ); if (response.statusCode == 200) { - return Directions.fromMap(jsonDecode(response.body)); + try { + return Directions.fromMap(jsonDecode(response.body)); + } on GoogleMapsException catch (_) { + return null; + } } } on HttpException catch (e) { debugPrint(e.message); diff --git a/lib/src/google_map.dart b/lib/src/google_map.dart index da125a3..da098b3 100644 --- a/lib/src/google_map.dart +++ b/lib/src/google_map.dart @@ -237,17 +237,20 @@ class _GoogleTrackTraceMapState extends State { key: widget.googleAPIKey, ) .then( - (value) => { - controller.route = TrackTraceRoute( - value.totalDuration, - value.totalDistance, - value.polylinePoints, - ), - checkDestinationCloseBy(), - controller.recenterCamera(), - setState(() { - lastRouteUpdate = DateTime.now(); - }), + (Directions? directions) => { + if (directions != null) + { + controller.route = TrackTraceRoute( + directions.totalDuration, + directions.totalDistance, + directions.polylinePoints, + ), + checkDestinationCloseBy(), + controller.recenterCamera(), + setState(() { + lastRouteUpdate = DateTime.now(); + }), + } }, ); }