mirror of
https://github.com/flutter/flutter
synced 2024-10-12 11:12:54 +00:00
Can call ChangeNotifier.hasListeners after disposed (#108931)
This commit is contained in:
parent
c22b7f697f
commit
6225bea261
|
@ -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() {
|
||||||
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);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue