autoComplete static method

Future<List<AutoCompleteResponseModel>> autoComplete(
  1. String query, {
  2. int? radius,
  3. bool? strictbounds,
  4. String? location,
})

Implementation

static Future<List<AutoCompleteResponseModel>> autoComplete(String query,
    {int? radius, bool? strictbounds, String? location}) {
  List<String> queryList = [
    "${baseUrl}autocomplete?input=$query",
    "api_key=$kApiKey",
    if (radius != null) "radius=$radius",
    if (strictbounds != null) "strictbounds=$strictbounds",
    if (location != null) "location=$location",
  ];
  return http
      .get(
    Uri.parse(queryList.join("&")),
  )
      .then((response) {
    List<AutoCompleteResponseModel> addresses = [];

    if (response.statusCode == 200) {
      var json = jsonDecode(response.body);
      for (var item in json['predictions']) {
        if (item is Map) {
          Map<String, dynamic> addressJson = {};
          addressJson['description'] = item['description'];
          addressJson['place_id'] = item['place_id'];
          if (item.containsKey("geometry")) {
            if (item['geometry'] is Map &&
                (item['geometry'] as Map).containsKey(
                  "location",
                )) {
              addressJson['lat'] = item['geometry']['location']['lat'];
              addressJson['lng'] = item['geometry']['location']['lng'];
            }
          }
          addresses.add(AutoCompleteResponseModel.fromJson(addressJson));
        }
      }
    }
    return addresses;
  });
}