Bläddra i källkod

fixed race condition bug in search

ashilkn 2 år sedan
förälder
incheckning
cfb0e5681c
2 ändrade filer med 7 tillägg och 10 borttagningar
  1. 6 6
      lib/ui/viewer/search/search_widget.dart
  2. 1 4
      lib/utils/debouncer.dart

+ 6 - 6
lib/ui/viewer/search/search_widget.dart

@@ -57,7 +57,6 @@ class _SearchWidgetState extends State<SearchWidget> {
 
   @override
   Widget build(BuildContext context) {
-    print('building search');
     return GestureDetector(
       onTap: () {
         Navigator.pop(context);
@@ -104,6 +103,8 @@ class _SearchWidgetState extends State<SearchWidget> {
                                 .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<SearchWidget> {
                             bool isDebouncing,
                             Widget child,
                           ) {
-                            print(_debouncer.debounceActiveNotifier.value);
                             return SearchSuffixIcon(
                               isDebouncing,
                             );
@@ -119,11 +119,13 @@ class _SearchWidgetState extends State<SearchWidget> {
                         ),
                       ),
                       onChanged: (value) async {
+                        _query = value;
                         final List<SearchResult> 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<SearchWidget> {
 
   @override
   void dispose() {
-    print('dispose');
     _debouncer.cancelDebounce();
     super.dispose();
   }
@@ -192,7 +193,6 @@ class _SearchWidgetState extends State<SearchWidget> {
 
     final monthResults = await _searchService.getMonthSearchResults(query);
     allResults.addAll(monthResults);
-
     completer.complete(allResults);
   }
 

+ 1 - 4
lib/utils/debouncer.dart

@@ -7,9 +7,7 @@ class Debouncer {
   final ValueNotifier<bool> _debounceActiveNotifier = ValueNotifier(false);
   Timer _debounceTimer;
 
-  Debouncer(this._duration) {
-    print('debounce instansiated');
-  }
+  Debouncer(this._duration);
 
   void run(Future<void> Function() fn) {
     if (isActive()) {
@@ -25,7 +23,6 @@ class Debouncer {
   void cancelDebounce() {
     if (_debounceTimer != null) {
       _debounceTimer.cancel();
-      print('cancelDebounce');
     }
   }