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) {
try {
return Directions.fromMap(jsonDecode(response.body)); 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) => {
if (directions != null)
{
controller.route = TrackTraceRoute( controller.route = TrackTraceRoute(
value.totalDuration, directions.totalDuration,
value.totalDistance, directions.totalDistance,
value.polylinePoints, directions.polylinePoints,
), ),
checkDestinationCloseBy(), checkDestinationCloseBy(),
controller.recenterCamera(), controller.recenterCamera(),
setState(() { setState(() {
lastRouteUpdate = DateTime.now(); lastRouteUpdate = DateTime.now();
}), }),
}
}, },
); );
} }