A Stream<T> delivers a sequence of values asynchronously. Stream.fromIterable(xs) builds a finite stream that emits each value in order and then completes. await for (var n in stream) consumes one value at a time inside an async function.

Program

Play the program to sum a finite stream by iterating it with await for.

stream_from_iterable.dart
Replay: real traced execution (multi-file project)
Future<int> sumStream(Stream<int> source) async {
  var total = 0;
  await for (var n in source) {
    total += n;
  }
  return total;
}

Future<void> main() async {
  var stream = Stream.fromIterable([1, 2, 3, 4]);
  var total = await sumStream(stream);
  print(total);
}
  1. stream ← Stream.fromIterable([1, 2, 3, 4])

    9Future<void> main() async {10  var stream = Stream.fromIterable([1, 2, 3, 4]);11  var total = await sumStream(stream);
    values this stepStream.fromIterable([1, 2, 3, 4])stream
  2. await ← suspended

    10var stream = Stream.fromIterable([1, 2, 3, 4]);11var total = await sumStream(stream);12print(total);
    values this stepsumStream(stream) suspendedawait
  3. total ← 0

    1Future<int> sumStream(Stream<int> source) async {2  var total = 0;3  await for (var n in source) {
    values this step0total
  4. stream event ← emit n = 1

    2var total = 0;3await for (var n in source) {4  total += n;
    values this stepemit n = 1stream event
  5. total ← 1

    3await for (var n in source) {4  total += n;5}
    values this step0 1total1n
  6. stream event ← emit n = 2

    2var total = 0;3await for (var n in source) {4  total += n;
    values this stepemit n = 2stream event
  7. total ← 3

    3await for (var n in source) {4  total += n;5}
    values this step1 3total2n
  8. stream event ← emit n = 3

    2var total = 0;3await for (var n in source) {4  total += n;
    values this stepemit n = 3stream event
  9. total ← 6

    3await for (var n in source) {4  total += n;5}
    values this step3 6total3n
  10. stream event ← emit n = 4

    2var total = 0;3await for (var n in source) {4  total += n;
    values this stepemit n = 4stream event
  11. total ← 10

    3await for (var n in source) {4  total += n;5}
    values this step6 10total4n
  12. stream event ← done (closed)

    2var total = 0;3await for (var n in source) {4  total += n;
    values this stepdone (closed)stream event
  13. return value ← 10

    5  }6  return total;7}
    values this step10return value10total
  14. await ← resolved total = 10

    10var stream = Stream.fromIterable([1, 2, 3, 4]);11var total = await sumStream(stream);12print(total);
    values this stepresolved total = 10await
  15. print(total);

    11  var total = await sumStream(stream);12  print(total);13}
    output10
    values this step10total
Stream.fromIterable `Stream.fromIterable(xs)` builds a finite stream that emits each value in order and then closes.
await for `await for (var n in source)` pauses the surrounding async function until the next value (or completion) is available.
loop completion When the stream signals it has no more values, the `await for` loop ends and the function continues.