fluttercommunity / flutter_google_places

Google Places - Google places autocomplete widgets for flutter. No wrapper, use https://pub.dev/packages/google_maps_webservice. Maintainer: @juliansteenbakker
https://pub.dev/packages/flutter_google_places
Other
303 stars 415 forks source link

getDetailsByPlaceId() not working after nullsafety upgrade #167

Closed lisaerikssonw closed 2 years ago

lisaerikssonw commented 3 years ago

Hi,

After upgrading to flutter_google_places: ^0.3.0, getting place details is not working. _places.autocomplete is working fine, but when I run getDetailsByPlaceId() using placeId from one of the Predictions, I get "type 'Null' is not a subtype of type 'String' in type cast".

_places = GoogleMapsPlaces(apiKey: _apiKey);

_autocompleteResponse = await _places.autocomplete(
      input,
      language: "se",
      components: [Component(Component.country, "se")],
      location: _position != null
          ? Location(lat: _position!.latitude, lng: _position!.longitude)
          : null,
      radius: _position != null ? 500 : null,
      types: ['address'],
    );

//...selected prediction is set - placeId is not null...

final placeId = selectedPrediction.placeId!;
final fields = ['address_component', 'geometry'];
print('Prediction placeId = $placeId');
      _detailsResponse =
          await _places.getDetailsByPlaceId(placeId, fields: fields);

Output:

I/flutter (12331): Prediction placeId = EiNTw6R0dGVyc3RhdsOkZ2VuLCBCYW5kaGFnZW4sIFN3ZWRlbiIuKiwKFAoSCY0tkZJ6d19GEZYmfy8J2pGaEhQKEgmbnnRUFHdfRhFamknR1lPT4g
E/flutter (12331): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: type 'Null' is not a subtype of type 'String' in type cast
E/flutter (12331): #0      _$PlaceDetailsFromJson
package:google_maps_webservice/src/places.g.dart:137
E/flutter (12331): #1      new PlaceDetails.fromJson
package:google_maps_webservice/src/places.dart:710
E/flutter (12331): #2      _$PlacesDetailsResponseFromJson
package:google_maps_webservice/src/places.g.dart:294
E/flutter (12331): #3      new PlacesDetailsResponse.fromJson
package:google_maps_webservice/src/places.dart:838
E/flutter (12331): #4      GoogleMapsPlaces._decodeDetailsResponse
package:google_maps_webservice/src/places.dart:528
E/flutter (12331): #5      GoogleMapsPlaces.getDetailsByPlaceId
package:google_maps_webservice/src/places.dart:126
E/flutter (12331): <asynchronous suspension>
E/flutter (12331): #6      _AddressAutocompleteFieldState._getPlaceDetails
package:itb_app/common/address_autocomplete.dart:254
E/flutter (12331): <asynchronous suspension>
E/flutter (12331):
lisaerikssonw commented 3 years ago

OK, I found a way to get around this by adding 'name' and 'place_id' fields in my details request, since they are non-nullable in your fromJson map. Maybe it should be clarified that these fields are mandatory when using field filters?

hoc081098 commented 3 years ago

Trying this: https://pub.dev/packages/flutter_google_places_hoc081098

juliansteenbakker commented 2 years ago

I have updated this package, please use the following:

  flutter_google_places:
    git:
      url: https://github.com/fluttercommunity/flutter_google_places
      ref: v0.3.2