onSearch method

void onSearch(
  1. String query
)

The onSearch function is called when the user types something in the search bar. It filters the cities based on the query. @param query The query string. {@category Functions}

Implementation

void onSearch(String query) {
  setState(() {
    filteredCities = cities
        .where(
            (city) => city.city.toLowerCase().contains(query.toLowerCase()))
        .toList();
    filteredCities.sort((a, b) => a.city.length.compareTo(b.city.length));
    filteredCities = filteredCities.take(3).toList();
    if (query.isEmpty) {
      setState(() {
        filteredCities = [];
      });
    }
  });
}