switchChildren method

Stream<Snapshot> switchChildren(
  1. Stream mapper(
    1. String key,
    2. Snapshot value
    )
)

Updates the content of each child with the values of the stream returned by mapper

Implementation

Stream<Snapshot> switchChildren(
        Stream<dynamic> Function(String key, Snapshot value) mapper) =>
    switchMap((s) {
      var keys = s.asMap()?.keys;
      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) {
        return l.fold(s, (s, e) => s.setPath(e.key, e.value));
      });
    });