Flutter SuggestionBuilder of SearchAnchor not updating when Riverpod Provider changes

115 views Asked by At

I'm building a searchbar using Flutter, Whenever the user searches something I need to do an API request to my backend to get the suggestions. When these suggestions are fetched I want to show them using the suggestionBuilder of the SearchAnchor like shown here:

  SearchAnchor stationSearchBar(PlannerViewModel plannerState) {
    plannerState.searchController.addListener(() {
      plannerState.searchInputChanged(plannerState.searchController.text);
    });
    return SearchAnchor(
        isFullScreen: false,
        searchController: plannerState.searchController,
        builder: (BuildContext context, SearchController controller) {
          return SearchBar(
              textCapitalization: TextCapitalization.none,
              controller: plannerState.searchController,
              onTap: () => plannerState.searchController.openView());
        },
        suggestionsBuilder:
            (BuildContext context, SearchController controller) {
          return [
            for (Station result in plannerState.searchResults)
              ListTile(
                title: Text(result.names.values.first),
              )
          ];
        });
  }

plannerState is a RiverPod provider, this is the function being used to fetch the suggestions:

Future<void> _searchStations(String query) async {
    print("searching $query");
    searchResults = await StationService().findStation(query);
    print("found ${searchResults.length}");
    notifyListeners();
  }

What happens is that when the user types something, firstly the suggestions list is rebuild (using the data from the previous search) and then the API request is performed, after the request, the suggestionBuilder doesnt rebuild its list. All the other state management work as normal. How can I fix this, can I force reload the suggestionBuilder somehow or something like that?

0

There are 0 answers