Lazy Iterables and Generators With sync*
Produce values on demand.
Lazy Iterables and Generators With sync* is a free Dart Academy lesson on CoddyKit — lesson 3 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.
What Lazy Means
A lazy iterable does not compute its values until you actually ask for them. Work happens on demand, not all at once. 🦥
Iterables Are Lazy
Methods like map and where return a lazy Iterable. Nothing runs until you iterate or call toList.
var it = [1, 2, 3].map((n) {
print('seen $n');
return n * 2;
});Nothing Runs Yet
Until you consume that iterable, the map body never executes. Laziness lets you describe work and defer the cost.
for (var x in it) print(x); // now it runsWhy Laziness Helps
Laziness saves work when you only need a few results. Combined with take, you can stop early and skip the rest.
var first2 = it.take(2).toList();Meet sync* Generators
A sync* function is a generator that builds a lazy Iterable. It produces values one at a time as they are requested.
Iterable<int> count(int n) sync* {
for (var i = 1; i <= n; i++) yield i;
}The yield Keyword
Inside a generator, yield hands one value back to the caller and pauses until the next value is needed.
print(count(3).toList()); // [1, 2, 3]Infinite Sequences
Because values are lazy, a generator can describe an infinite sequence safely, as long as you take only a finite slice.
Iterable<int> naturals() sync* {
var i = 0;
while (true) yield ++i;
}Taking a Finite Slice
Pair an infinite generator with take to grab just what you need. The loop stops the moment your slice is filled.
print(naturals().take(5).toList()); // [1, 2, 3, 4, 5]yield* Delegates
Use yield* to emit every value from another iterable or generator. It flattens one sequence into the current one.
Iterable<int> both() sync* {
yield* count(2);
yield* count(2);
}Generators vs Building Lists
A generator avoids building a full list in memory. You stream values, which keeps big or endless sequences memory-light.
sync* vs async*
Use sync* for synchronous lazy iterables. Its async sibling, async*, produces a Stream for asynchronous event sequences.
Quick Check
Test your understanding of generators.
Recap
Iterables are lazy, and sync* generators yield values on demand. That lets you model even infinite sequences cheaply. 🌟
Frequently asked questions
Is the “Lazy Iterables and Generators With sync*” lesson free?
Yes — the full text of “Lazy Iterables and Generators With sync*” 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 “Lazy Iterables and Generators With sync*”?
Produce values on demand. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Lazy Iterables and Generators With sync*” 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
- Higher-Order Functions and Currying
- fold, reduce, and expand
- Lazy Iterables and Generators With sync*
- Composing Pure Function Pipelines