From 7b5ec588d12d4e794e2d5af80ae2bbe051bc4594 Mon Sep 17 00:00:00 2001 From: Nate Date: Mon, 26 Feb 2024 16:52:23 -0700 Subject: [PATCH] Allow `Listenable.merge()` to use any iterable (#143675) This is a very small change that fixes #143664. --- .../lib/src/foundation/change_notifier.dart | 10 +++++----- .../test/foundation/change_notifier_test.dart | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/packages/flutter/lib/src/foundation/change_notifier.dart b/packages/flutter/lib/src/foundation/change_notifier.dart index 27ef987ef76..7cf4c00d3ca 100644 --- a/packages/flutter/lib/src/foundation/change_notifier.dart +++ b/packages/flutter/lib/src/foundation/change_notifier.dart @@ -62,11 +62,11 @@ abstract class Listenable { /// Return a [Listenable] that triggers when any of the given [Listenable]s /// themselves trigger. /// - /// The list must not be changed after this method has been called. Doing so - /// will lead to memory leaks or exceptions. + /// Once the factory is called, items must not be added or removed from the iterable. + /// Doing so will lead to memory leaks or exceptions. /// - /// The list may contain nulls; they are ignored. - factory Listenable.merge(List listenables) = _MergingListenable; + /// The iterable may contain nulls; they are ignored. + factory Listenable.merge(Iterable listenables) = _MergingListenable; /// Register a closure to be called when the object notifies its listeners. void addListener(VoidCallback listener); @@ -491,7 +491,7 @@ mixin class ChangeNotifier implements Listenable { class _MergingListenable extends Listenable { _MergingListenable(this._children); - final List _children; + final Iterable _children; @override void addListener(VoidCallback listener) { diff --git a/packages/flutter/test/foundation/change_notifier_test.dart b/packages/flutter/test/foundation/change_notifier_test.dart index aa41dc3beb9..55ef811f8c9 100644 --- a/packages/flutter/test/foundation/change_notifier_test.dart +++ b/packages/flutter/test/foundation/change_notifier_test.dart @@ -314,6 +314,21 @@ void main() { log.clear(); }); + test('Merging change notifiers supports any iterable', () { + final TestNotifier source1 = TestNotifier(); + final TestNotifier source2 = TestNotifier(); + final List log = []; + + final Listenable merged = Listenable.merge({source1, source2}); + void listener() => log.add('listener'); + + merged.addListener(listener); + source1.notify(); + source2.notify(); + expect(log, ['listener', 'listener']); + log.clear(); + }); + test('Merging change notifiers ignores null', () { final TestNotifier source1 = TestNotifier(); final TestNotifier source2 = TestNotifier();