onAutoCompletion method

Future<void> onAutoCompletion(
  1. String term,
  2. String googleMapApiKey,
  3. String language
)

Fetches autocomplete suggestions based on the given search term.

This method queries the repository for suggestions and updates the state.

  • term: The search term entered by the user.
  • googleMapApiKey: The API key for accessing Google Places API.
  • language: The language code for the autocomplete suggestions.

Implementation

Future<void> onAutoCompletion(
    String term, String googleMapApiKey, String language) async {
  state.isLoading = true;
  try {
    final response =
        await _repository.onAutoCompletion(term, googleMapApiKey, language);

    if (response.status == 'ZERO_RESULTS') {
      // No results were found for the given term
      state.autocompletionList = [];
    } else if (response.status == 'REQUEST_DENIED') {
      // Handle request denial if necessary
      if (kDebugMode) {
        print('Request denied: ${response.error}');
      }
    } else {
      // Update the state with the list of predictions
      state.autocompletionList = response.predictions;
    }
  } catch (e) {
    // Log the error if debugging
    if (kDebugMode) {
      print('Error during autocomplete: $e');
    }
  } finally {
    // Always set loading to false after the request completes
    state.isLoading = false;
  }
}