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';
/// get the route between the two coordinates
Future<Directions> getDirections({
Future<Directions?> getDirections({
required LatLng origin,
required LatLng destination,
required TravelMode mode,
@ -32,7 +32,11 @@ class DirectionsRepository {
},
);
if (response.statusCode == 200) {
try {
return Directions.fromMap(jsonDecode(response.body));
} on GoogleMapsException catch (_) {
return null;
}
}
} on HttpException catch (e) {
debugPrint(e.message);

View file

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