Reputation: 1
I'm trying to use flutter_graphql to fetch data from my custom endpoint using a simple Query methodology. This worked until I introduced a SearchAnchor class to allow a user to perform a search which would change some of the variables and perform a refetch on the endpoint. This now produces an error:
FlutterError (A SearchController was used after being disposed. Once you have called dispose() on a SearchController, it can no longer be used.)
This seems to be produced by the ValueNotifier
class used to instantiate the GraphQL provider but the SearchAnchor does not automatically handle disposing of the SearchAnchor before a refetch is performed. Does anyone know how to solve this?
Here's some code to reproduce the issue: Searchbar controller:
return Container(
margin: const EdgeInsets.only(top: 10, left: 50),
child: SearchAnchor(
builder: (BuildContext context, SearchController textController) {
return SearchBar(
controller: textController,
hintText: 'Search for a city, state or zip code',
onTap: () {
textController.openView();
},
onChanged: (_) {
textController.openView();
},
);
},
suggestionsBuilder: (BuildContext context, SearchController searchController) async {
List suggestions = [];
final String search = searchController.text.toLowerCase();
//............
return List<ListTile>.generate(textList.length, (int index) {
final item = textList[index];
return ListTile(
title: Text(item["location"]),
onTap: () {
widget.setInitialPosition(LatLng(item["latitude"], item["longitude"]));
setState(() {
searchController.closeView(item["location"]);
});
},
);
}).toList();
}
);
Map controller:
body: Query(
// query set up goes here
builder: (QueryResult result, { Refetch? refetch, FetchMore? fetchMore }) {
void setInitialPosition(LatLng position) async {
_lastMapPosition = position;
// THIS throws the error
await refetch!();
}
}
Expected: A user would perform a search in the search bar, select an option, GraphQL Provider would perform a refetch with new variables.
Actul: A user performs a search in the search bar, selects an option, an error is thrown: "FlutterError (A SearchController was used after being disposed. Once you have called dispose() on a SearchController, it can no longer be used.)"
Upvotes: 0
Views: 56