From 16b7348155c327a87d36570b2a1532c7b3ecfd70 Mon Sep 17 00:00:00 2001 From: Greg Spencer Date: Tue, 31 May 2022 10:33:46 -0700 Subject: [PATCH] Switch debugAssertNotDisposed to be a static (#104772) This reverts part of the change made in #103456 to expose a debug check for subclasses of ChangeNotifier to avoid code duplication. Instead of making debugAssertNotDisposed a public instance function, it is now a public static function. It makes it harder to call, slightly, but it means that everyone who implemented ChangeNotifier instead of extending it doesn't get broken. --- .../lib/src/foundation/change_notifier.dart | 23 +++++++++++-------- .../flutter/lib/src/widgets/restoration.dart | 10 ++++---- .../flutter/lib/src/widgets/shortcuts.dart | 6 ++--- .../test/foundation/change_notifier_test.dart | 23 +++++++++++++++++++ 4 files changed, 44 insertions(+), 18 deletions(-) diff --git a/packages/flutter/lib/src/foundation/change_notifier.dart b/packages/flutter/lib/src/foundation/change_notifier.dart index 2a7daea75d..2b22a7fd98 100644 --- a/packages/flutter/lib/src/foundation/change_notifier.dart +++ b/packages/flutter/lib/src/foundation/change_notifier.dart @@ -129,19 +129,22 @@ class ChangeNotifier implements Listenable { /// ```dart /// class MyNotifier with ChangeNotifier { /// void doUpdate() { - /// assert(debugAssertNotDisposed()); + /// assert(ChangeNotifier.debugAssertNotDisposed(this)); /// // ... /// } /// } /// ``` /// {@end-tool} - @protected - bool debugAssertNotDisposed() { + // This is static and not an instance method because too many people try to + // implement ChangeNotifier instead of extending it (and so it is too breaking + // to add a method, especially for debug). + static bool debugAssertNotDisposed(ChangeNotifier notifier) { assert(() { - if (_debugDisposed) { + if (notifier._debugDisposed) { throw FlutterError( - 'A $runtimeType was used after being disposed.\n' - 'Once you have called dispose() on a $runtimeType, it can no longer be used.', + 'A ${notifier.runtimeType} was used after being disposed.\n' + 'Once you have called dispose() on a ${notifier.runtimeType}, it ' + 'can no longer be used.', ); } return true; @@ -166,7 +169,7 @@ class ChangeNotifier implements Listenable { /// so, stopping that same work. @protected bool get hasListeners { - assert(debugAssertNotDisposed()); + assert(ChangeNotifier.debugAssertNotDisposed(this)); return _count > 0; } @@ -198,7 +201,7 @@ class ChangeNotifier implements Listenable { /// the list of closures that are notified when the object changes. @override void addListener(VoidCallback listener) { - assert(debugAssertNotDisposed()); + assert(ChangeNotifier.debugAssertNotDisposed(this)); if (_count == _listeners.length) { if (_count == 0) { _listeners = List.filled(1, null); @@ -293,7 +296,7 @@ class ChangeNotifier implements Listenable { /// This method should only be called by the object's owner. @mustCallSuper void dispose() { - assert(debugAssertNotDisposed()); + assert(ChangeNotifier.debugAssertNotDisposed(this)); assert(() { _debugDisposed = true; return true; @@ -321,7 +324,7 @@ class ChangeNotifier implements Listenable { @visibleForTesting @pragma('vm:notify-debugger-on-exception') void notifyListeners() { - assert(debugAssertNotDisposed()); + assert(ChangeNotifier.debugAssertNotDisposed(this)); if (_count == 0) { return; } diff --git a/packages/flutter/lib/src/widgets/restoration.dart b/packages/flutter/lib/src/widgets/restoration.dart index bae89fdf31..ea865f414b 100644 --- a/packages/flutter/lib/src/widgets/restoration.dart +++ b/packages/flutter/lib/src/widgets/restoration.dart @@ -473,7 +473,7 @@ abstract class RestorableProperty extends ChangeNotifier { @override void dispose() { - assert(debugAssertNotDisposed()); // FYI, This uses ChangeNotifier's _debugDisposed, not _disposed. + assert(ChangeNotifier.debugAssertNotDisposed(this)); // FYI, This uses ChangeNotifier's _debugDisposed, not _disposed. _owner?._unregister(this); super.dispose(); _disposed = true; @@ -483,14 +483,14 @@ abstract class RestorableProperty extends ChangeNotifier { String? _restorationId; RestorationMixin? _owner; void _register(String restorationId, RestorationMixin owner) { - assert(debugAssertNotDisposed()); + assert(ChangeNotifier.debugAssertNotDisposed(this)); assert(restorationId != null); assert(owner != null); _restorationId = restorationId; _owner = owner; } void _unregister() { - assert(debugAssertNotDisposed()); + assert(ChangeNotifier.debugAssertNotDisposed(this)); assert(_restorationId != null); assert(_owner != null); _restorationId = null; @@ -503,14 +503,14 @@ abstract class RestorableProperty extends ChangeNotifier { @protected State get state { assert(isRegistered); - assert(debugAssertNotDisposed()); + assert(ChangeNotifier.debugAssertNotDisposed(this)); return _owner!; } /// Whether this property is currently registered with a [RestorationMixin]. @protected bool get isRegistered { - assert(debugAssertNotDisposed()); + assert(ChangeNotifier.debugAssertNotDisposed(this)); return _restorationId != null; } } diff --git a/packages/flutter/lib/src/widgets/shortcuts.dart b/packages/flutter/lib/src/widgets/shortcuts.dart index 2179da4d44..a51335c3a8 100644 --- a/packages/flutter/lib/src/widgets/shortcuts.dart +++ b/packages/flutter/lib/src/widgets/shortcuts.dart @@ -1092,7 +1092,7 @@ class ShortcutRegistry with ChangeNotifier { /// /// Returns a copy: modifying the returned map will have no effect. Map get shortcuts { - assert(debugAssertNotDisposed()); + assert(ChangeNotifier.debugAssertNotDisposed(this)); return { for (final MapEntry> entry in _tokenShortcuts.entries) ...entry.value, @@ -1123,7 +1123,7 @@ class ShortcutRegistry with ChangeNotifier { /// * [ShortcutRegistryEntry.dispose], a function used to remove the set of /// shortcuts associated with a particular entry. ShortcutRegistryEntry addAll(Map value) { - assert(debugAssertNotDisposed()); + assert(ChangeNotifier.debugAssertNotDisposed(this)); final ShortcutRegistryEntry entry = ShortcutRegistryEntry._(this); _tokenShortcuts[entry] = value; assert(_debugCheckForDuplicates()); @@ -1191,7 +1191,7 @@ class ShortcutRegistry with ChangeNotifier { // Replaces all the shortcuts associated with the given entry from this // registry. void _replaceAll(ShortcutRegistryEntry entry, Map value) { - assert(debugAssertNotDisposed()); + assert(ChangeNotifier.debugAssertNotDisposed(this)); assert(_debugCheckTokenIsValid(entry)); _tokenShortcuts[entry] = value; assert(_debugCheckForDuplicates()); diff --git a/packages/flutter/test/foundation/change_notifier_test.dart b/packages/flutter/test/foundation/change_notifier_test.dart index 7b348e1151..cfdf4aa59f 100644 --- a/packages/flutter/test/foundation/change_notifier_test.dart +++ b/packages/flutter/test/foundation/change_notifier_test.dart @@ -475,6 +475,29 @@ void main() { ); }); + test('Calling debugAssertNotDisposed works as intended', () { + final TestNotifier testNotifier = TestNotifier(); + expect(ChangeNotifier.debugAssertNotDisposed(testNotifier), isTrue); + testNotifier.dispose(); + FlutterError? error; + try { + ChangeNotifier.debugAssertNotDisposed(testNotifier); + } on FlutterError catch (e) { + error = e; + } + expect(error, isNotNull); + expect(error, isFlutterError); + expect( + error!.toStringDeep(), + equalsIgnoringHashCodes( + 'FlutterError\n' + ' A TestNotifier was used after being disposed.\n' + ' Once you have called dispose() on a TestNotifier, it can no\n' + ' longer be used.\n', + ), + ); + }); + test('notifyListener can be called recursively', () { final Counter counter = Counter(); final List log = [];