getSearchSuggestions method

List<String> getSearchSuggestions(
  1. String query,
  2. String type, {
  3. int limit = 5,
})

Get search suggestions with intelligent ranking and caching

Implementation

List<String> getSearchSuggestions(String query, String type, {int limit = 5}) {
  final suggestions = <String>[];
  query = query.toLowerCase();

  switch (type.toLowerCase()) {
    case 'country':
      _countrySearchIndex?.forEach((prefix, countries) {
        if (prefix.startsWith(query) && suggestions.length < limit) {
          suggestions.add(countries.first.name);
        }
      });
      break;
    case 'state':
      _stateSearchIndex?.forEach((prefix, states) {
        if (prefix.startsWith(query) && suggestions.length < limit) {
          suggestions.add(states.first.name);
        }
      });
      break;
    case 'city':
      _citySearchIndex?.forEach((prefix, cities) {
        if (prefix.startsWith(query) && suggestions.length < limit) {
          suggestions.add(cities.first.name);
        }
      });
      break;
  }

  return suggestions;
}