Skip to content

Commit 7b5ec58

Browse files
Allow Listenable.merge() to use any iterable (#143675)
This is a very small change that fixes #143664.
1 parent c82ca46 commit 7b5ec58

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

packages/flutter/lib/src/foundation/change_notifier.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ abstract class Listenable {
6262
/// Return a [Listenable] that triggers when any of the given [Listenable]s
6363
/// themselves trigger.
6464
///
65-
/// The list must not be changed after this method has been called. Doing so
66-
/// will lead to memory leaks or exceptions.
65+
/// Once the factory is called, items must not be added or removed from the iterable.
66+
/// Doing so will lead to memory leaks or exceptions.
6767
///
68-
/// The list may contain nulls; they are ignored.
69-
factory Listenable.merge(List<Listenable?> listenables) = _MergingListenable;
68+
/// The iterable may contain nulls; they are ignored.
69+
factory Listenable.merge(Iterable<Listenable?> listenables) = _MergingListenable;
7070

7171
/// Register a closure to be called when the object notifies its listeners.
7272
void addListener(VoidCallback listener);
@@ -491,7 +491,7 @@ mixin class ChangeNotifier implements Listenable {
491491
class _MergingListenable extends Listenable {
492492
_MergingListenable(this._children);
493493

494-
final List<Listenable?> _children;
494+
final Iterable<Listenable?> _children;
495495

496496
@override
497497
void addListener(VoidCallback listener) {

packages/flutter/test/foundation/change_notifier_test.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,21 @@ void main() {
314314
log.clear();
315315
});
316316

317+
test('Merging change notifiers supports any iterable', () {
318+
final TestNotifier source1 = TestNotifier();
319+
final TestNotifier source2 = TestNotifier();
320+
final List<String> log = <String>[];
321+
322+
final Listenable merged = Listenable.merge(<Listenable?>{source1, source2});
323+
void listener() => log.add('listener');
324+
325+
merged.addListener(listener);
326+
source1.notify();
327+
source2.notify();
328+
expect(log, <String>['listener', 'listener']);
329+
log.clear();
330+
});
331+
317332
test('Merging change notifiers ignores null', () {
318333
final TestNotifier source1 = TestNotifier();
319334
final TestNotifier source2 = TestNotifier();

0 commit comments

Comments
 (0)