fquery icon indicating copy to clipboard operation
fquery copied to clipboard

A Timer is still pending even after the widget tree was disposed

Open AhmedHathout opened this issue 1 year ago • 1 comments

Hi, I am facing a problem with widget tests. If the widget I am testing has a UseQueryResult, the test fails because of a pending timer. Here is the stack trace:-

Pending timers:
Timer (duration: 0:03:00.000000, periodic: false), created:
#0      new FakeTimer._ (package:fake_async/fake_async.dart:308:62)
#1      FakeAsync._createTimer (package:fake_async/fake_async.dart:252:27)
...
...
...
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following assertion was thrown running a test:
A Timer is still pending even after the widget tree was disposed.
'package:flutter_test/src/binding.dart':
Failed assertion: line 1542 pos 12: '!timersPending'

When the exception was thrown, this was the stack:
#2      AutomatedTestWidgetsFlutterBinding._verifyInvariants (package:flutter_test/src/binding.dart:1542:12)
#3      TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:1049:7)
<asynchronous suspension>
<asynchronous suspension>
(elided 3 frames from class _AssertionError and package:stack_trace)

It looks like it is because of the cacheDuration (the one that if I change its value, the second line in the stack trace I provided change). I tried to set the cacheDuration to 0 but I get the same error. Is there a way to either dispose that timer (and other timers if there are any) or not creating it in tests from the beginning?

AhmedHathout avatar Sep 03 '24 12:09 AhmedHathout

Any advice on how to fix this one?

AhmedHathout avatar Sep 10 '24 02:09 AhmedHathout

this has been fixed in 92c035aab0b2f2301c81d54b9e26828c7e92f087 🙌

41y08h avatar Mar 28 '25 12:03 41y08h

Thank you. I tried it again but I still face the same problem

Pending timers:
Timer (duration: 0:00:00.000000, periodic: false), created:
#0      new FakeTimer._ (package:fake_async/fake_async.dart:308:62)
#1      FakeAsync._createTimer (package:fake_async/fake_async.dart:252:29)
#2      FakeAsync.run.<anonymous closure> (package:fake_async/fake_async.dart:185:19)
#6      Observer.onQueryUpdated (package:fquery/src/observer.dart:156:12)
#7      Query._notifyListeners (package:fquery/src/query.dart:178:16)
#8      Query.dispatch (package:fquery/src/query.dart:143:5)
#9      Observer.fetch (package:fquery/src/observer.dart:131:11)
#10     Observer.initialize (package:fquery/src/observer.dart:67:7)
#11     useQuery.<anonymous closure>.<anonymous closure> (package:fquery/src/hooks/use_query.dart:161:16)
...
...
...
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following assertion was thrown running a test:
A Timer is still pending even after the widget tree was disposed.
'package:flutter_test/src/binding.dart':
Failed assertion: line 1606 pos 12: '!timersPending'

I am using Dart 3.7.2, Flutter 3.29.3 and fquery 1.5.4-beta.2

Am I doing something wrong?

AhmedHathout avatar May 06 '25 23:05 AhmedHathout