fetchSuggestions method

Future<List<Suggestion>?> fetchSuggestions(
  1. String input,
  2. String lang
)

Implementation

Future<List<Suggestion>?> fetchSuggestions(String input, String lang) async {
  String _apiKey =
      _remoteConfigService!.getValue(RemoteConfigKey.googlePlaceAPIKey);

  final request = Uri.parse(
      'https://maps.googleapis.com/maps/api/place/autocomplete/json?input=$input&language=$lang&key=$_apiKey&sessiontoken=$_sessionToken');
  final response = await client.get(request);

  if (response.statusCode == 200) {
    print("Whoop");
    final result = json.decode(response.body);
    print("Decoded the response");
    if (result['status'] == 'OK') {
      print("Whoop whoop");
      // compose suggestions in a list
      return result['predictions']
          .map<Suggestion>((p) => Suggestion(p['place_id'], p['description']))
          .toList();
    }
    if (result['status'] == 'ZERO_RESULTS') {
      print("Rip whoop");
      return [];
    }
    print(result);
    print(result['status']);
    throw Exception(result['error_message']);
  } else {
    print("Response not good");
    print("Error");
    print("$response");
    throw Exception('Failed to fetch suggestion');
  }
}