'Place Picker Null Result
I am using Flutter Place Picker to select a location but the results are null.
I have used the example provided but I am getting NULL result with "Unnamed Location" as per the photo below;
void showPlacePicker() async {
LocationResult result = await Navigator.of(context).push(MaterialPageRoute(
builder: (context) =>
PlacePicker("AIzaSyBJ4Q0lFgGPr6at5IXD6j5YLAFuAKe1Nbo")));
print(result);
}
Solution 1:[1]
In addition to enabling Places API, Maps SDK for Android and Maps SDK for iOS for the API key, you should also enable Geocoding API. I hope the documentation will be updated.
Solution 2:[2]
Follow the steps below and your issue will be solved:
In the code below press CTRL(cmd for MAC) on PlacePicker() and navigate to place_picker.dart file:
void showPlacePicker() async { LocationResult result = await Navigator.of(context).push(MaterialPageRoute( builder: (context) => PlacePicker("YOUR_API_KEY"))); print(result);
}
Inside place_picker.dart file of the library replace the whole method
void reverseGeocodeLatLng(LatLng latLng) async{....}
with the following:void reverseGeocodeLatLng(LatLng latLng) async { try { final response = await http.get( "https://maps.googleapis.com/maps/api/geocode/json?" + "latlng=${latLng.latitude},${latLng.longitude}&" + "key=${widget.apiKey}"); if (response.statusCode != 200) { throw Error(); } final responseJson = jsonDecode(response.body); print('Google maps response body ' + response.body); if (responseJson['results'] == null) { throw Error(); } final result = responseJson['results'][0]; setState(() { if (result['address_components'] is List && result['address_components'].length >= 7) { this.locationResult = LocationResult() ..name = result['address_components'][0]['short_name'] ..locality = result['address_components'][1]['short_name'] ..latLng = latLng ..formattedAddress = result['formatted_address'] ..placeId = result['place_id'] ..postalCode = result['address_components'][7]['short_name'] ..country = AddressComponent.fromJson( result['address_components'][6]) ..administrativeAreaLevel1 = AddressComponent.fromJson( result['address_components'][5]) ..administrativeAreaLevel2 = AddressComponent.fromJson( result['address_components'][4]) ..city = AddressComponent.fromJson(result['address_components'][3]) ..subLocalityLevel1 = AddressComponent.fromJson( result['address_components'][2]) ..subLocalityLevel2 = AddressComponent.fromJson( result['address_components'][1]); } else { this.locationResult = LocationResult() ..latLng = latLng ..formattedAddress = result['formatted_address'] ..placeId = result['place_id'] ..name = result['address_components'][0]['short_name'] ..locality = result['address_components'][1]['short_name'] ..administrativeAreaLevel1 = AddressComponent.fromJson( result['address_components'][2]) ..city = AddressComponent.fromJson(result['address_components'][3]) ..country = AddressComponent.fromJson( result['address_components'][4]); } }); } catch (e) { print(e); }
}
Close the app. Uninstall it and Run it once again your problem will be solved.
Note: Maps SDK for Android, Places API, and Geocoding API must be enabled for using place_picker.
Credit: https://github.com/blackmann/locationpicker/issues/29
Solution 3:[3]
Solution 4:[4]
Try this.
Void showPlacePicker() async {
LocationResult result = await Navigator.of(context).push(MaterialPageRoute(
builder: (context) =>
PlacePicker("your_api_key",
displayLocation: customLocation,
)));
print(result);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tim Kariuki |
Solution 2 | |
Solution 3 | Ricardo |
Solution 4 | A.R.H |