autoCompleteSearch method
Fetches the place autocomplete list with the query place
.
Implementation
void autoCompleteSearch(String place) async {
try {
place = place.replaceAll(" ", "+");
var endpoint =
"https://maps.googleapis.com/maps/api/place/autocomplete/json?"
"key=${widget.apiKey}&"
"language=${widget.localizationItem!.languageCode}&"
"input={$place}&sessiontoken=${this.sessionToken}";
if (this.locationResult != null) {
endpoint += "&location=${this.locationResult!.latLng?.latitude}," +
"${this.locationResult!.latLng?.longitude}";
}
final response = await http.get(Uri.parse(endpoint));
if (response.statusCode != 200) {
throw Error();
}
final responseJson = jsonDecode(response.body);
if (responseJson['predictions'] == null) {
throw Error();
}
List<dynamic> predictions = responseJson['predictions'];
List<RichSuggestion> suggestions = [];
if (predictions.isEmpty) {
AutoCompleteItem aci = AutoCompleteItem();
aci.text = widget.localizationItem!.noResultsFound;
aci.offset = 0;
aci.length = 0;
suggestions.add(RichSuggestion(aci, () {}));
} else {
for (dynamic t in predictions) {
final aci = AutoCompleteItem()
..id = t['place_id']
..text = t['description']
..offset = t['matched_substrings'][0]['offset']
..length = t['matched_substrings'][0]['length'];
suggestions.add(RichSuggestion(aci, () {
FocusScope.of(context).requestFocus(FocusNode());
decodeAndSelectPlace(aci.id!);
}));
}
}
displayAutoCompleteSuggestions(suggestions);
} catch (e) {
print(e);
}
}