I've implemented the search feature on google map, for that used google_places_flutter library. below is the implemented code,
GooglePlaceAutoCompleteTextField(
textEditingController: _searchController,
googleAPIKey: mapKey,
isCrossBtnShown: false,
radius: 16,
inputDecoration: const InputDecoration(
hintText: "Search Location...",
border: UnderlineInputBorder(
borderSide:
BorderSide(color: Colors.black54),
borderRadius: BorderRadius.zero,
),
enabledBorder: UnderlineInputBorder(
borderSide:
BorderSide(color: Colors.black54),
borderRadius: BorderRadius.zero,
),
focusedBorder: UnderlineInputBorder(
borderSide:
BorderSide(color: Colors.blue),
),
focusedErrorBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.red),
borderRadius: BorderRadius.zero,
),
contentPadding: EdgeInsets.only(left: 12.0),
fillColor: Colors.transparent,
filled: false,
),
boxDecoration: BoxDecoration(
borderRadius: BorderRadius.circular(0.0),
),
debounceTime: 800,
isLatLngRequired: true,
getPlaceDetailWithLatLng: onPlaceSelected,
),
void onPlaceSelected(dynamic place) async {
double lat = place['geometry']['location']['lat'];
double lng = place['geometry']['location']['lng'];
await _updateLocation(lat, lng);
}
while searching it, the api gives the drop down list and while clicking on the item it is giving the below error, I think this error is not from my code.
Let me know if any more information is required.
The issue is caused by the library.
Here's the corrected code: We need to add the itemClick
callback, which gets triggered when isLatLngRequired
is true. However, even with this callback, the latitude and longitude might still be null. But when clicking on an item, both itemClick
and getPlaceDetailWithLatLng
are called, and getPlaceDetailWithLatLng
contains the correct lat and long data.
So, just add an empty itemClick
callback, and it should start working.
GooglePlaceAutoCompleteTextField(
textEditingController: _searchController,
googleAPIKey: mapKey,
//...Other
debounceTime: 800,
isLatLngRequired: true,
getPlaceDetailWithLatLng: _onPlaceSelected,
itemClick: (Prediction prediction) {},
),
void _onPlaceSelected(Prediction place) async {
double? lat = double.tryParse(place.lat ?? "0.0");
double? lng = double.tryParse(place.lng ?? "0.0");
}