Improve change notifier (#4747)

This patch improves some subtle behaviors about the change notifier.
This commit is contained in:
Adam Barth
2016-06-24 15:53:48 -07:00
committed by GitHub
parent 490622b4da
commit b00efda7fc
2 changed files with 9 additions and 6 deletions

View File

@@ -30,23 +30,26 @@ abstract class ChangeNotifier {
/// This method should only be called by the object's owner.
@mustCallSuper
void dispose() {
_listeners = null;
_listeners = const <VoidCallback>[];
}
/// Call all the registered listeners.
///
/// Call this method whenever the object changes, to notify any clients the
/// object may have.
/// object may have. Listeners that are added during this iteration will not
/// be visited. Listeners that are removed during this iteration will not be
/// visited after they are removed.
///
/// Exceptions thrown by listeners will be caught and reported using
/// [FlutterError.reportError].
@protected
void notifyListeners() {
if (_listeners != null) {
List<VoidCallback> listeners = new List<VoidCallback>.from(_listeners);
for (VoidCallback listener in listeners) {
List<VoidCallback> localListeners = new List<VoidCallback>.from(_listeners);
for (VoidCallback listener in localListeners) {
try {
listener();
if (_listeners.contains(listener))
listener();
} catch (exception, stack) {
FlutterError.reportError(new FlutterErrorDetails(
exception: exception,

View File

@@ -102,7 +102,7 @@ void main() {
test.addListener(listener2);
test.addListener(listener3);
test.notify();
expect(log, equals(<String>['listener1', 'listener2', 'listener3']));
expect(log, equals(<String>['listener1', 'listener2']));
log.clear();
test.notify();