Created
August 23, 2019 14:31
-
-
Save rullyalves/777ff0544e3e0e35d15cf3b347a81a16 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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