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

Not-found addresses cause The method '[]' was called on null. #124

Open
willbryant opened this issue Jun 12, 2021 · 1 comment
Open

Not-found addresses cause The method '[]' was called on null. #124

willbryant opened this issue Jun 12, 2021 · 1 comment

Comments

@willbryant
Copy link

If you make a distance matrix call that returns any NOT_FOUND cells, which happens if you give an address that is both incomplete and invalid (eg. "1 Nonexistant Street" with no city), distance.dart throws The method '[]' was called on null. because it tries to dereference JSON keys that don't exist.

Here's the stacktrace:

#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
#1      _$ValueFromJson (package:google_maps_webservice/src/distance.g.dart:66:16)
#2      new Value.fromJson (package:google_maps_webservice/src/distance.dart:328:56)
#3      _$ElementFromJson (package:google_maps_webservice/src/distance.g.dart:52:21)
#4      new Element.fromJson (package:google_maps_webservice/src/distance.dart:317:7)
#5      _$RowFromJson.<anonymous closure> (package:google_maps_webservice/src/distance.g.dart:40:34)
#6      MappedListIterable.elementAt (dart:_internal/iterable.dart:412:31)
#7      ListIterator.moveNext (dart:_internal/iterable.dart:341:26)
#8      new _GrowableList._ofEfficientLengthIterable (dart:core-patch/growable_array.dart:188:27)
#9      new _GrowableList.of (dart:core-patch/growable_array.dart:150:28)
#10     new List.of (dart:core-patch/array_patch.dart:50:28)
#11     ListIterable.toList (dart:_internal/iterable.dart:212:44)
#12     _$RowFromJson (package:google_maps_webservice/src/distance.g.dart:41:14)
#13     new Row.fromJson (package:google_maps_webservice/src/distance.dart:300:54)
#14     _$DistanceResponseFromJson.<anonymous closure> (package:google_maps_webservice/src/distance.g.dart:22:30)
#15     MappedListIterable.elementAt (dart:_internal/iterable.dart:412:31)
#16     ListIterator.moveNext (dart:_internal/iterable.dart:341:26)
#17     new _GrowableList._ofEfficientLengthIterable (dart:core-patch/growable_array.dart:188:27)
#18     new _GrowableList.of (dart:core-patch/growable_array.dart:150:28)
#19     new List.of (dart:core-patch/array_patch.dart:50:28)
#20     ListIterable.toList (dart:_internal/iterable.dart:212:44)
#21     _$DistanceResponseFromJson (package:google_maps_webservice/src/distance.g.dart:23:14)
#22     new DistanceResponse.fromJson (package:google_maps_webservice/src/distance.dart:289:7)
#23     GoogleDistanceMatrix._decode (package:google_maps_webservice/src/distance.dart:254:24)
#24     GoogleDistanceMatrix._distance (package:google_maps_webservice/src/distance.dart:60:12)
<asynchronous suspension>
#25     StackZoneSpecification._registerUnaryCallback.<anonymous closure> (package:stack_trace/src/stack_zone_specification.dart)
<asynchronous suspension>

And here's an example maps API response showing the NOT_FOUND elements:

{
   "destination_addresses" : [ "1 Tasman Street, Mount Cook, Wellington 6021, New Zealand", "" ],
   "origin_addresses" : [
      "Elizabeth Street at Kent Terrace, Wellington 6011, New Zealand",
      "1 Tasman Street, Mount Cook, Wellington 6021, New Zealand",
      ""
   ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "1.2 km",
                  "value" : 1159
               },
               "duration" : {
                  "text" : "3 mins",
                  "value" : 205
               },
               "status" : "OK"
            },
            {
               "status" : "NOT_FOUND"
            }
         ]
      },
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "1 m",
                  "value" : 0
               },
               "duration" : {
                  "text" : "1 min",
                  "value" : 0
               },
               "status" : "OK"
            },
            {
               "status" : "NOT_FOUND"
            }
         ]
      },
      {
         "elements" : [
            {
               "status" : "NOT_FOUND"
            },
            {
               "status" : "NOT_FOUND"
            }
         ]
      }
   ],
   "status" : "OK"
}
@willbryant
Copy link
Author

I think the issue is here:

class Element {
  final Value distance;
  final Value duration;
  final String? elementStatus;

distance and duration both need to have type Value? as they are not present in the NOT_FOUND case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant