Unverified Commit 6225bea2 authored by chunhtai's avatar chunhtai Committed by GitHub

Can call ChangeNotifier.hasListeners after disposed (#108931)

parent c22b7f69
...@@ -170,11 +170,10 @@ class ChangeNotifier implements Listenable { ...@@ -170,11 +170,10 @@ class ChangeNotifier implements Listenable {
/// [notifyListeners]; and similarly, by overriding [removeListener], checking /// [notifyListeners]; and similarly, by overriding [removeListener], checking
/// if [hasListeners] is false after calling `super.removeListener()`, and if /// if [hasListeners] is false after calling `super.removeListener()`, and if
/// so, stopping that same work. /// so, stopping that same work.
///
/// This method returns false if [dispose] has been called.
@protected @protected
bool get hasListeners { bool get hasListeners => _count > 0;
assert(ChangeNotifier.debugAssertNotDisposed(this));
return _count > 0;
}
/// Register a closure to be called when the object changes. /// Register a closure to be called when the object changes.
/// ///
......
...@@ -349,6 +349,20 @@ void main() { ...@@ -349,6 +349,20 @@ void main() {
expect(error, isNull); expect(error, isNull);
}); });
test('Can check hasListener on a disposed ChangeNotifier', () {
final HasListenersTester<int> source = HasListenersTester<int>(0);
source.addListener(() { });
expect(source.testHasListeners, isTrue);
FlutterError? error;
try {
source.dispose();
expect(source.testHasListeners, isFalse);
} on FlutterError catch (e) {
error = e;
}
expect(error, isNull);
});
test('Value notifier', () { test('Value notifier', () {
final ValueNotifier<double> notifier = ValueNotifier<double>(2.0); final ValueNotifier<double> notifier = ValueNotifier<double>(2.0);
......
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