File tree 2 files changed +22
-0
lines changed
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 {
321
321
@mustCallSuper
322
322
void dispose () {
323
323
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
+ );
324
330
assert (() {
325
331
_debugDisposed = true ;
326
332
return true ;
Original file line number Diff line number Diff line change @@ -49,6 +49,22 @@ class Counter with ChangeNotifier {
49
49
}
50
50
51
51
void 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
+
52
68
testWidgets ('ChangeNotifier' , (WidgetTester tester) async {
53
69
final List <String > log = < String > [];
54
70
void listener () {
You can’t perform that action at this time.
0 commit comments