Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle NOT_FOUND addresses in Distance Matrix API responses (closes #124) #125

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/src/distance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

Expand Down
8 changes: 6 additions & 2 deletions lib/src/distance.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 35 additions & 4 deletions test/distance_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -493,12 +493,28 @@ Future<void> 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);
});
});
});
Expand All @@ -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'
};