Fix route calculation error when no route has been found

This commit is contained in:
Bart Ribbers 2022-11-03 15:38:01 +01:00
parent f7b455e53e
commit 1951e6a4fd
2 changed files with 20 additions and 13 deletions

View file

@ -6,7 +6,7 @@ class DirectionsRepository {
static const String _baseUrl = '/maps/api/directions/json'; static const String _baseUrl = '/maps/api/directions/json';
/// get the route between the two coordinates /// get the route between the two coordinates
Future<Directions> getDirections({ Future<Directions?> getDirections({
required LatLng origin, required LatLng origin,
required LatLng destination, required LatLng destination,
required TravelMode mode, required TravelMode mode,
@ -32,7 +32,11 @@ class DirectionsRepository {
}, },
); );
if (response.statusCode == 200) { 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) { } on HttpException catch (e) {
debugPrint(e.message); debugPrint(e.message);

View file

@ -237,17 +237,20 @@ class _GoogleTrackTraceMapState extends State<GoogleTrackTraceMap> {
key: widget.googleAPIKey, key: widget.googleAPIKey,
) )
.then( .then(
(value) => { (Directions? directions) => {
controller.route = TrackTraceRoute( if (directions != null)
value.totalDuration, {
value.totalDistance, controller.route = TrackTraceRoute(
value.polylinePoints, directions.totalDuration,
), directions.totalDistance,
checkDestinationCloseBy(), directions.polylinePoints,
controller.recenterCamera(), ),
setState(() { checkDestinationCloseBy(),
lastRouteUpdate = DateTime.now(); controller.recenterCamera(),
}), setState(() {
lastRouteUpdate = DateTime.now();
}),
}
}, },
); );
} }