Python Synchronous Generators: sync* and yield
When you need to lazily produce a sequence of values without computing the entire list in memory upfront, Dart provides **Synchronous Generators** (`sync*`). Generator functions return an `Iterable<T>` and use the `yield` keyword to emit values one at a time as the caller requests them.
"A synchronous generator is like a soft-serve ice cream dispenser: it doesn't pre-pour 100 cones into a freezer; it dispenses one fresh cone only when you pull the lever."
Deep Dive: How It Works
`sync*` Modifier: Marks the function as a synchronous generator returning `Iterable<T>`.
`yield`: Emits a single value to the consumer and pauses function execution until the next item is requested.
`yield*`: Delegates emission to another nested `Iterable` (`yield* getSubRange();`).
Lazy Evaluation: Code inside a `sync*` function does not execute until the consumer begins iterating over the returned `Iterable`.
Syntax Blueprint
Iterable<int> countUpTo(int max) sync* {
for (var i = 1; i <= max; i++) {
yield i; // Emits i and pauses
}
}Synchronous generator emitting on-demand sequence values.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Using `return value;` inside a `sync*` generator.Why it happens: Generators emit values via `yield`, not `return`.
How to fix: Use `yield value;` to emit and bare `return;` to exit the generator early.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Create Countdown Generator
Define generator `Iterable<int> countdown(int from) sync* { while (from > 0) { yield from; from--; } }`.
In `main()`, iterate through `countdown(3)` and print each number.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.