From 5eb6b1108b3484649ee7b4f98f0004c7df09aa00 Mon Sep 17 00:00:00 2001 From: Will Bryant Date: Sun, 13 Jun 2021 12:33:02 +1200 Subject: [PATCH] Handle NOT_FOUND addresses in Distance Matrix API responses --- lib/src/distance.dart | 8 ++++---- lib/src/distance.g.dart | 8 ++++++-- test/distance_test.dart | 39 +++++++++++++++++++++++++++++++++++---- 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/lib/src/distance.dart b/lib/src/distance.dart index 74f798b..1e4083e 100644 --- a/lib/src/distance.dart +++ b/lib/src/distance.dart @@ -303,13 +303,13 @@ class Row { @JsonSerializable() class Element { - final Value distance; - final Value duration; + final Value? distance; + final Value? duration; final String? elementStatus; Element({ - required this.distance, - required this.duration, + this.distance, + this.duration, this.elementStatus, }); diff --git a/lib/src/distance.g.dart b/lib/src/distance.g.dart index a3685e4..2f9f08c 100644 --- a/lib/src/distance.g.dart +++ b/lib/src/distance.g.dart @@ -49,8 +49,12 @@ Map _$RowToJson(Row instance) => { Element _$ElementFromJson(Map json) { return Element( - distance: Value.fromJson(json['distance'] as Map), - duration: Value.fromJson(json['duration'] as Map), + distance: json['distance'] == null + ? null + : Value.fromJson(json['distance'] as Map), + duration: json['duration'] == null + ? null + : Value.fromJson(json['duration'] as Map), elementStatus: json['element_status'] as String?, ); } diff --git a/test/distance_test.dart b/test/distance_test.dart index 54d28a9..f0a6b75 100644 --- a/test/distance_test.dart +++ b/test/distance_test.dart @@ -493,12 +493,28 @@ Future main() async { expect(response.destinationAddresses.first, equals('Rd 11, Dhaka 1212, Bangladesh')); expect(response.rows.first.elements, hasLength(1)); - expect(response.rows.first.elements.first.duration.text, + expect(response.rows.first.elements.first.duration?.text, equals('29 mins')); - expect(response.rows.first.elements.first.duration.value, equals(1725)); + expect(response.rows.first.elements.first.duration?.value, equals(1725)); expect( - response.rows.first.elements.first.distance.text, equals('9.2 km')); - expect(response.rows.first.elements.first.distance.value, equals(9247)); + response.rows.first.elements.first.distance?.text, equals('9.2 km')); + expect(response.rows.first.elements.first.distance?.value, equals(9247)); + }); + + test('decode NOT_FOUND response', () { + var response = DistanceResponse.fromJson(_notFoundResponseExample); + + expect(response.isOkay, isTrue); + expect(response.rows, hasLength(1)); + expect(response.originAddresses, hasLength(1)); + expect(response.originAddresses.first, + equals('Bakshi Bazar Road, Dhaka, Bangladesh')); + expect(response.destinationAddresses, hasLength(1)); + expect(response.destinationAddresses.first, + equals('')); + expect(response.rows.first.elements, hasLength(1)); + expect(response.rows.first.elements.first.duration, null); + expect(response.rows.first.elements.first.distance, null); }); }); }); @@ -520,3 +536,18 @@ final _responseExample = { ], 'status': 'OK' }; + +final _notFoundResponseExample = { + 'destination_addresses': [''], + 'origin_addresses': ['Bakshi Bazar Road, Dhaka, Bangladesh'], + 'rows': [ + { + 'elements': [ + { + 'status': 'NOT_FOUND' + } + ] + } + ], + 'status': 'OK' +};