How to correctly use ChangeNotifier
with SearchDelegate
?
I have something that looks like this:
class SearchNotifier with ChangeNotifier {
List<String> results;
Future<void> search(String query) async {
results = await API.search(query);
notifyListeners();
}
}
And in my SearchDelegate
:
Widget buildSuggestions(BuildContext context) {
final searchNotifier = Provider.of<SearchNotifier>(context);
searchNotifier.search(query);
...
}
When the results update, SearchNotifier
updates its listeners, SearchDelegate
is rebuild, buildSuggestions
is called and search
is called again, entering in a loop.
Is there a way of doing searchNotifier.search(query)
outside a build method? Maybe somehow I can add a listener to SearchDelegate
_queryTextController
?
I'm using provider to inject my SearchNotifier
, so wherever call search
we need to have access to the context.