Unverified Commit 3a3a0db1 authored by chunhtai's avatar chunhtai Committed by GitHub

Disallow dispose during listener callback (#114530)

* Disallow dispose during listener callback

* addressing comment

* add comments to code

* Addressing comments

* fix test
parent 7fd3bb2a
......@@ -321,6 +321,12 @@ class ChangeNotifier implements Listenable {
@mustCallSuper
void dispose() {
assert(ChangeNotifier.debugAssertNotDisposed(this));
assert(
_notificationCallStackDepth == 0,
'The "dispose()" method on $this was called during the call to '
'"notifyListeners()". This is likely to cause errors since it modifies '
'the list of listeners while the list is being used.',
);
assert(() {
_debugDisposed = true;
return true;
......
......@@ -49,6 +49,22 @@ class Counter with ChangeNotifier {
}
void main() {
testWidgets('ChangeNotifier can not dispose in callback', (WidgetTester tester) async {
final TestNotifier test = TestNotifier();
bool callbackDidFinish = false;
void foo() {
test.dispose();
callbackDidFinish = true;
}
test.addListener(foo);
test.notify();
final AssertionError error = tester.takeException() as AssertionError;
expect(error.toString().contains('dispose()'), isTrue);
// Make sure it crashes during dispose call.
expect(callbackDidFinish, isFalse);
});
testWidgets('ChangeNotifier', (WidgetTester tester) async {
final List<String> log = <String>[];
void listener() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment