Commit b00efda7 authored by Adam Barth's avatar Adam Barth Committed by GitHub

Improve change notifier (#4747)

This patch improves some subtle behaviors about the change notifier.
parent 490622b4
......@@ -30,22 +30,25 @@ 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 {
if (_listeners.contains(listener))
listener();
} catch (exception, stack) {
FlutterError.reportError(new FlutterErrorDetails(
......
......@@ -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();
......
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