Stream Use a Stream to handle multiple async events

This lesson is available for PRO members or as a single course purchase. Sign-in and choose a plan below.
Get Unlimited PRO Access


*Enrollment provides full access to this course (and updates) for life.

Create a Stream

A Stream provides a way to handle multiple async events. Simulate a stream of events by creating one from a list of numbers.

file_type_dartlang streams.dart
var stream = Stream.fromIterable([1, 2, 3]);

By default, a Stream can only have one listener. If you intend to have multiple listeners convert it to a broadcast stream.

file_type_dartlang streams.dart
var stream = Stream.fromIterable([1, 2, 3]).asBroadcastStream();

Listen to a Stream

Listen to a Stream by subscribing to it with the listen method. The callback function will be fired for each event emitted by the Stream.

file_type_dartlang streams.dart
stream.listen((event) => print(event));

Async For Loop

We can also use the async/await syntax to listen to a stream - think of it like a for loop that unfolds over the dimension of time.

file_type_dartlang streams.dart
streamFun() async {
  var stream = Stream.fromIterable([4, 5, 6]);

  await for (int value in stream) {


Ask questions via GitHub below OR chat on Slack #questions