Skip to content

Commit

Permalink
fix: switchChildren not emitting any value when empty map
Browse files Browse the repository at this point in the history
  • Loading branch information
rbellens committed Feb 26, 2024
1 parent 2b50b44 commit 5bcb261
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/src/stream.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ extension SnapshotStreamX on Stream<Snapshot> {
Stream<dynamic> Function(String key, Snapshot value) mapper) =>
switchMap((s) {
var keys = s.asMap()?.keys;
if (keys == null) return Stream.value(s);
if (keys == null || keys.isEmpty) return Stream.value(s);
return CombineLatestStream(
keys.map((k) => mapper(k, s.child(k)).map((v) => MapEntry(k, v))),
(List<MapEntry<String, dynamic>> l) {
Expand Down
9 changes: 9 additions & 0 deletions test/stream_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,15 @@ void main() async {
expect((await stream.first).as(),
{'jane': 'Jane Doe', 'john': 'John Doe'});
});

test('with empty map', () async {
var stream = Stream<Snapshot>.value(Snapshot.fromJson({}));

stream = stream.switchChildren((key, value) => Stream.value(
'${value.child('firstname').as()} ${value.child('lastname').as()}'));

expect((await stream.first).as(), {});
});
});
group('Stream<Snapshot>.recycle', () {
test('Stream<Snapshot>.recycle', () async {
Expand Down

0 comments on commit 5bcb261

Please sign in to comment.