import 'package:latlong2/latlong.dart'; class Directions { final List segments; final List waypointsCoordinates; const Directions({required this.segments, required this.waypointsCoordinates}); factory Directions.fromJson(Map json) { return Directions( segments: _getSegmentsFromJson(json), waypointsCoordinates: _getWaypointsCoordinatesFromJson(json), ); } static List _getSegmentsFromJson(Map json) { final segmentsJson = List>.from(json['features'][0]['properties']['segments'][0]['steps']); final List segments = []; for (final segmentJson in segmentsJson) { segments.add(DirectionSegment.fromJson(segmentJson)); } return segments; } static List _getWaypointsCoordinatesFromJson(Map json) { final coordinatesJson = List.from(json['features'][0]['geometry']['coordinates']); final List waypointsCoordinates = []; for (final coordinateJson in coordinatesJson) { waypointsCoordinates.add(LatLng(coordinateJson[1], coordinateJson[0])); } return waypointsCoordinates; } } class DirectionSegment { final num distance; final num duration; final int type; final String instruction; final List waypoints; const DirectionSegment({ required this.distance, required this.duration, required this.type, required this.instruction, required this.waypoints, }); factory DirectionSegment.fromJson(Map json) { return DirectionSegment( distance: json['distance'], duration: json['duration'], type: json['type'], instruction: json['instruction'], waypoints: List.from(json['way_points']), ); } }