This repository was archived by the owner on Feb 22, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +22
-0
lines changed Expand file tree Collapse file tree 2 files changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -321,6 +321,12 @@ class ChangeNotifier implements Listenable {
321321 @mustCallSuper
322322 void dispose () {
323323 assert (ChangeNotifier .debugAssertNotDisposed (this ));
324+ assert (
325+ _notificationCallStackDepth == 0 ,
326+ 'The "dispose()" method on $this was called during the call to '
327+ '"notifyListeners()". This is likely to cause errors since it modifies '
328+ 'the list of listeners while the list is being used.' ,
329+ );
324330 assert (() {
325331 _debugDisposed = true ;
326332 return true ;
Original file line number Diff line number Diff line change @@ -49,6 +49,22 @@ class Counter with ChangeNotifier {
4949}
5050
5151void main () {
52+ testWidgets ('ChangeNotifier can not dispose in callback' , (WidgetTester tester) async {
53+ final TestNotifier test = TestNotifier ();
54+ bool callbackDidFinish = false ;
55+ void foo () {
56+ test.dispose ();
57+ callbackDidFinish = true ;
58+ }
59+
60+ test.addListener (foo);
61+ test.notify ();
62+ final AssertionError error = tester.takeException () as AssertionError ;
63+ expect (error.toString ().contains ('dispose()' ), isTrue);
64+ // Make sure it crashes during dispose call.
65+ expect (callbackDidFinish, isFalse);
66+ });
67+
5268 testWidgets ('ChangeNotifier' , (WidgetTester tester) async {
5369 final List <String > log = < String > [];
5470 void listener () {
You can’t perform that action at this time.
0 commit comments