Disallow dispose during listener callback (#114530)
* Disallow dispose during listener callback * addressing comment * add comments to code * Addressing comments * fix test
This commit is contained in:
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user