Collection if and for
Build lists conditionally and in loops.
Collection if and for 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.
Logic Inside Literals
Sometimes a list's contents depend on a condition. Dart lets you put an if right inside the collection literal, so you build it in one place.
Collection if
A collection if adds an element only when its condition is true. No condition met means that slot simply contributes nothing.
var nav = ['Home', if (loggedIn) 'Profile'];if With else
You can add an else to choose between two elements. One of the two always lands in the collection, never both.
var tags = [if (vip) 'Gold' else 'Standard'];No Braces Needed
Unlike a statement-level if, the collection if takes no curly braces and no semicolons. It produces an element, it does not run a block.
Collection for
A collection for generates elements by looping, right inside the literal. Each pass adds whatever the loop body yields.
var doubled = [for (var n in nums) n * 2];Like map, but Inline
A collection for often replaces a map call when you are already inside a literal. It can read more directly for simple transforms.
Mixing if and for
You can combine them: loop with for, then filter with if. Together they build a shaped list in a single expressive literal.
var evens = [for (var n in nums) if (n.isEven) n];Works Beyond Lists
Collection if and for also work in set and map literals. The same friendly syntax shapes any of the three collection kinds.
var m = {for (var u in users) u.id: u.name};Pairs With Spread
You can mix these with the spread operator. Inline some items, spread another collection, and add conditionals, all in one literal.
var x = [...base, if (extra) 'plus'];Why It Beats Building After
Building the collection up front keeps it declarative. You avoid creating an empty list and pushing into it with separate statements.
Great for UI Lists
This shines in Flutter, where a children list often shows widgets conditionally or from data. The literal stays clean and readable. 🧩
Quick Check
What does a collection if do when its condition is false and no else is given?
Recap
Collection if adds elements conditionally and collection for generates them in a loop, right inside list, set, and map literals. 🎯
Frequently asked questions
Is the “Collection if and for” lesson free?
Yes — the full text of “Collection if and for” 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 “Collection if and for”?
Build lists conditionally and in loops. 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 “Collection if and for” 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
- The Cascade Operator (..)
- Null-Shorting Cascades (?..)
- Spread and Null-Aware Spread
- Collection if and for