autocomplete method

Future<TGoogleAutocompleteResponse> autocomplete(
  1. String input, {
  2. TGooglePlacesConfig? config,
  3. List<String> fieldMasks = defaultAutocompleteFieldMasks,
})

Get auto-complete suggestions from Google Places API.

Implementation

Future<TGoogleAutocompleteResponse> autocomplete(
  String input, {
  TGooglePlacesConfig? config,
  List<String> fieldMasks = defaultAutocompleteFieldMasks,
}) async {
  final apiKey = _resolveApiKey();
  if (apiKey == null || apiKey.isEmpty) {
    throw Exception('Google Map API Key not resolved.');
  }

  final data = (config ?? const TGooglePlacesConfig()).toJson(input);

  final response = await _dio.post(
    'https://places.googleapis.com/v1/places:autocomplete',
    data: data,
    options: Options(
      headers: {
        'X-Goog-Api-Key': apiKey,
        'X-Goog-FieldMask': fieldMasks.join(','),
        'Content-Type': 'application/json',
      },
    ),
  );

  if (response.data is Map<String, dynamic>) {
    return TGoogleAutocompleteResponse.fromJson(response.data as Map<String, dynamic>);
  }
  throw Exception('Invalid response format from Autocomplete API.');
}