diff --git a/lib/ui/viewer/search/search_widget.dart b/lib/ui/viewer/search/search_widget.dart index 643bb63dd..bef79bc74 100644 --- a/lib/ui/viewer/search/search_widget.dart +++ b/lib/ui/viewer/search/search_widget.dart @@ -57,7 +57,6 @@ class _SearchWidgetState extends State { @override Widget build(BuildContext context) { - print('building search'); return GestureDetector( onTap: () { Navigator.pop(context); @@ -104,6 +103,8 @@ class _SearchWidgetState extends State { .withOpacity(0.5), ), ), + /*Using valueListenableBuilder inside a stateful widget because this widget is only rebuild when + setState is called when deboucncing is over and the spinner needs to be shown while debouncing */ suffixIcon: ValueListenableBuilder( valueListenable: _debouncer.debounceActiveNotifier, builder: ( @@ -111,7 +112,6 @@ class _SearchWidgetState extends State { bool isDebouncing, Widget child, ) { - print(_debouncer.debounceActiveNotifier.value); return SearchSuffixIcon( isDebouncing, ); @@ -119,11 +119,13 @@ class _SearchWidgetState extends State { ), ), onChanged: (value) async { + _query = value; final List allResults = await getSearchResultsForQuery(value); - if (mounted) { + /*checking if _query == value to make sure that the results are from the current query + and not from the previous query (race condition).*/ + if (mounted && _query == value) { setState(() { - _query = value; _results.clear(); _results.addAll(allResults); }); @@ -148,7 +150,6 @@ class _SearchWidgetState extends State { @override void dispose() { - print('dispose'); _debouncer.cancelDebounce(); super.dispose(); } @@ -192,7 +193,6 @@ class _SearchWidgetState extends State { final monthResults = await _searchService.getMonthSearchResults(query); allResults.addAll(monthResults); - completer.complete(allResults); } diff --git a/lib/utils/debouncer.dart b/lib/utils/debouncer.dart index 87de90351..e4ad0e16d 100644 --- a/lib/utils/debouncer.dart +++ b/lib/utils/debouncer.dart @@ -7,9 +7,7 @@ class Debouncer { final ValueNotifier _debounceActiveNotifier = ValueNotifier(false); Timer _debounceTimer; - Debouncer(this._duration) { - print('debounce instansiated'); - } + Debouncer(this._duration); void run(Future Function() fn) { if (isActive()) { @@ -25,7 +23,6 @@ class Debouncer { void cancelDebounce() { if (_debounceTimer != null) { _debounceTimer.cancel(); - print('cancelDebounce'); } }