fetchSuggestions function

Future<List<AddressSuggestion>> fetchSuggestions(
  1. Client client,
  2. String apiKey,
  3. String input, {
  4. List<PlaceType>? types,
  5. List? regionCodes,
  6. String? sessionToken,
  7. String? languageCode,
})

Implementation

Future<List<AddressSuggestion>> fetchSuggestions(
  http.Client client,
  String apiKey,
  String input, {
  List<PlaceType>? types,
  List? regionCodes,
  String? sessionToken,
  String? languageCode,
}) async {
  if (input.isEmpty || input.length < 3) {
    return [];
  }
  final queryParams = {
    if (languageCode != null) 'languageCode': languageCode,
    if (sessionToken != null) 'sessionToken': sessionToken,
  };
  String request = '$baseUrl/places:autocomplete';
  final headers = {
    'Content-Type': 'application/json',
    "X-Goog-Api-Key": apiKey,
  };
  var body = jsonEncode({
    "input": input,
    if (regionCodes != null) "includedRegionCodes": regionCodes,
  });
  var uri = Uri.parse(request).replace(queryParameters: queryParams);
  final response = await client.post(uri, headers: headers, body: body);
  final Map<String, dynamic> result = jsonDecode(
    utf8.decode(response.bodyBytes),
  );

  if (response.statusCode == 200) {
    if (result['suggestions'] == null) {
      log.i("No suggestions found for input=$input");
      return [];
    }
    List<AddressSuggestion> addressSuggestions =
        result['suggestions'].map<AddressSuggestion>((suggestion) {
          return AddressSuggestion.fromJson(suggestion);
        }).toList();
    return addressSuggestions;
  } else if (response.statusCode == 400 && result["error"] != null) {
    var exception = ApiException.fromJson(result["error"]);
    throw exception;
  } else {
    log.e(
      "Failed fetching for input=$input: Http code ${response.statusCode}, Body: ${response.body}",
    );
    throw Exception('Failed to fetch suggestion for input=$input.');
  }
}