Skip to content

Instantly share code, notes, and snippets.

@rullyalves
Created August 23, 2019 14:31
Show Gist options
  • Select an option

  • Save rullyalves/777ff0544e3e0e35d15cf3b347a81a16 to your computer and use it in GitHub Desktop.

Select an option

Save rullyalves/777ff0544e3e0e35d15cf3b347a81a16 to your computer and use it in GitHub Desktop.
class SearchNotifier extends ChangeNotifier {
final GithubApi _api;
Timer _timer;
CancelableOperation<SearchResult> _operation;
SearchResult _result;
SearchResult get result => _result;
set result(SearchResult result) {
_result = result;
notifyListeners();
}
SearchNotifier({GithubApi api}) : _api = api ?? GithubApi();
void search(String term) {
if (term.isEmpty) {
_result = null;
return;
}
_timer?.cancel();
_operation?.cancel();
_timer = Timer(Duration(milliseconds: 350), () async {
result = null;
_operation = CancelableOperation.fromFuture(_api.search(term));
try {
final _result = await _operation.value;
if (_result.isEmpty) {
result = null;
} else {
result = _result;
}
} catch (e) {
result = null;
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment