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
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);
}
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.