0Pricing
Dart Academy · Lesson

Creating Streams With async*

Yield events from a generator.

Creating Streams With async* is a free Dart Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Producing Your Own Streams

So far you consumed streams others made. Now you will produce them yourself using an async generator, the cleanest way to emit events.

The async* Keyword

Mark a function async* and it becomes a stream generator. Its return type is a Stream, and it can emit values over time. 🚿

Stream<int> countTo(int n) async* {
  // body uses yield
}

yield Emits a Value

Inside an async* function, yield sends one value into the stream. Execution pauses there until the listener is ready for more.

Stream<int> oneTwo() async* {
  yield 1;
  yield 2;
}

yield in a Loop

Combine yield with a loop to emit many values. Each pass sends the next number into the stream automatically.

Stream<int> countTo(int n) async* {
  for (var i = 1; i <= n; i++) yield i;
}

await Inside async*

Because the function is async, you can await between yields. This lets you fetch or delay before emitting the next event.

Stream<int> ticker() async* {
  while (true) {
    await Future.delayed(Duration(seconds: 1));
    yield DateTime.now().second;
  }
}

yield* Forwards a Stream

Use yield* to emit every event of another stream from inside your generator. It splices that whole stream into yours.

Stream<int> combined() async* {
  yield* countTo(3);
  yield 99;
}

Lazy Until Listened

An async* function does not start running until someone listens. The body only executes as the listener pulls each value. ⏸️

Pausing Respects the Listener

If a listener pauses, the generator pauses too at its next yield. This backpressure keeps producers from overwhelming consumers.

Errors From async*

A throw inside an async* function becomes an error event on the stream. Listeners can catch it just like any other stream error.

Stream<int> risky() async* {
  yield 1;
  throw 'boom';
}

Consuming Your Generator

Read your generated stream the usual way with await for. Each yielded value flows into the loop in order.

await for (final n in countTo(3)) {
  print(n);
}

async* vs StreamController

For pull-based, sequential output, async* is simpler than a StreamController. Reach for a controller only when events come from outside callbacks.

Quick Check

How do you emit a value from an async* function?

Recap

You built streams with async*, sending values via yield, forwarding with yield*, and awaiting between events. You can now both consume and produce streams. 🎉

Frequently asked questions

Is the “Creating Streams With async*” lesson free?

Yes — the full text of “Creating Streams With async*” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.

What will I learn in “Creating Streams With async*”?

Yield events from a generator. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Dart Academy?

No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Creating Streams With async*” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Dart Academy lesson?

Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Listening With await for
  2. Single vs Broadcast Streams
  3. Transforming Streams: map, where, take
  4. Creating Streams With async*
← Back to Dart Academy