getAutoCompletePlaces method

Future<PlacesPredictions> getAutoCompletePlaces({
  1. String? sessionToken,
  2. String? input,
  3. required String types,
  4. required String countries,
})

Implementation

Future<PlacesPredictions> getAutoCompletePlaces(
    {String? sessionToken,
      String? input,
      required String types,
    required String countries}) async {
  Map<String, String> requestHeaders = {
    HttpHeaders.contentTypeHeader: "application/json",
    HttpHeaders.acceptHeader: "application/json",
  };
  Map<String, String> queryParameters = {
    'sessiontoken': "$sessionToken",
    'input': "$input",
    'key': "$apiKey",
    //'types': "$types",
    'components': "${convertCountryListToString(countries)}",
  };
  Uri uri = Uri.https(
      endpoint, "maps/api/place/autocomplete/json", queryParameters);
  final response = await client.get(uri, headers: requestHeaders);
  if (response.statusCode == 200 ||
      response.statusCode == 201 ||
      response.statusCode == 203 ||
      response.statusCode == 204) {
    return PlacesPredictions.fromJson(json.decode(response.body));
  } else if (response.body.isNotEmpty) {
    throw Exception(response.body);
  } else {
    throw Exception('${response.toString()}');
  }
}