← → or space · progress saves for Continue on the roadmap
Goal
Derive new streams without manual controllers when transforms fit.
Step 1 - map
import 'dart:async';
Future<void> main() async {
final s = Stream.fromIterable([1, 2, 3]).map((n) => 'n=$n');
await for (final v in s) {
print(v);
}
}Step 2 - where
import 'dart:async';
Future<void> main() async {
final s = Stream.fromIterable([1, 2, 3, 4]).where((n) => n.isEven);
print(await s.toList());
}Step 3 - Chaining
import 'dart:async';
Future<void> main() async {
final out = Stream.fromIterable([1, 2, 3, 4])
.where((n) => n.isOdd)
.map((n) => n * 10);
await for (final v in out) {
print(v);
}
}Good habit
- Transforms are lazy: nothing runs until someone listens or pulls with
await for.
Practice tasks
- Add
distinct()frompackage:asyncin a real project, or implement a tinydistinctextension that tracks the last value (optional). - Build
Stream<String>from ints withmapand filter empty strings withwhere.