From 9ece6daff64106843166717de0e6beb1c00d8c81 Mon Sep 17 00:00:00 2001 From: Ian Hickson Date: Tue, 17 Jan 2017 14:17:00 -0800 Subject: [PATCH] Add more asserts and docs to ChangeNotifier. (#7513) It took me a while to figure out what was going on (I was removing a listener after disposal). These asserts helped. --- .../lib/src/foundation/change_notifier.dart | 35 +++++++++++++++++-- .../test/foundation/change_notifier_test.dart | 9 +++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/foundation/change_notifier.dart b/packages/flutter/lib/src/foundation/change_notifier.dart index 1814b8fa21..f09dd2d058 100644 --- a/packages/flutter/lib/src/foundation/change_notifier.dart +++ b/packages/flutter/lib/src/foundation/change_notifier.dart @@ -32,29 +32,57 @@ abstract class Listenable { /// A class that can be extended or mixed in that provides a change notification /// API using [VoidCallback] for notifications. +/// +/// [ChangeNotifier] is optimised for small numbers (one or two) of listeners. +/// It is O(N) for adding and removing listeners and O(N²) for dispatching +/// notifications (where N is the number of listeners). class ChangeNotifier extends Listenable { List _listeners; + bool _debugAssertNotDisposed() { + assert(() { + if (_listeners == const []) { + throw new FlutterError( + 'A $runtimeType was used after being disposed.\n' + 'Once you have called dispose() on a $runtimeType, it can no longer be used.' + ); + } + return true; + }); + return true; + } + /// Register a closure to be called when the object changes. + /// + /// This method must not be called after [dispose] has been called. @override void addListener(VoidCallback listener) { + assert(_debugAssertNotDisposed); _listeners ??= []; _listeners.add(listener); } /// Remove a previously registered closure from the list of closures that are /// notified when the object changes. + /// + /// If the given listener is not registered, the call is ignored. + /// + /// This method must not be called after [dispose] has been called. @override void removeListener(VoidCallback listener) { + assert(_debugAssertNotDisposed); _listeners?.remove(listener); } - /// Discards any resources used by the object. After this is called, the object - /// is not in a usable state and should be discarded. + /// Discards any resources used by the object. After this is called, the + /// object is not in a usable state and should be discarded (calls to + /// [addListener] and [removeListener] will throw after the object is + /// disposed). /// /// This method should only be called by the object's owner. @mustCallSuper void dispose() { + assert(_debugAssertNotDisposed); _listeners = const []; } @@ -67,8 +95,11 @@ class ChangeNotifier extends Listenable { /// /// Exceptions thrown by listeners will be caught and reported using /// [FlutterError.reportError]. + /// + /// This method must not be called after [dispose] has been called. @protected void notifyListeners() { + assert(_debugAssertNotDisposed); if (_listeners != null) { List localListeners = new List.from(_listeners); for (VoidCallback listener in localListeners) { diff --git a/packages/flutter/test/foundation/change_notifier_test.dart b/packages/flutter/test/foundation/change_notifier_test.dart index 09adeadbff..24cb71bf47 100644 --- a/packages/flutter/test/foundation/change_notifier_test.dart +++ b/packages/flutter/test/foundation/change_notifier_test.dart @@ -181,4 +181,13 @@ void main() { source2.notify(); expect(log, isEmpty); }); + + test('Cannot use a disposed ChangeNotifier', () { + final TestNotifier source = new TestNotifier(); + source.dispose(); + expect(() { source.addListener(null); }, throwsFlutterError); + expect(() { source.removeListener(null); }, throwsFlutterError); + expect(() { source.dispose(); }, throwsFlutterError); + expect(() { source.notify(); }, throwsFlutterError); + }); }