Introduction
Collection if is a Dart feature that lets you conditionally include elements in a collection literal using an if expression directly inside the collection. No temporary variables or multi-step logic needed.
Why It Matters
Building dynamic lists or maps where certain items should only be included based on a condition is a common task. Collection if keeps this logic inline and readable, eliminating the need to build the collection in multiple steps.
Syntax
var list = [
element1,
if (condition) element2,
if (condition) element3 else element4,
];
Code Example
void main() {
bool isAdmin = true;
bool hasPremium = false;
// Conditionally add menu items
var menuItems = [
'Home',
'Profile',
if (isAdmin) 'Admin Panel',
if (hasPremium) 'Premium Content' else 'Upgrade to Premium',
];
print(menuItems);
// In a Map
String? promoCode;
var order = {
'product': 'Dart Book',
'price': 29.99,
if (promoCode != null) 'promo': promoCode,
};
print(order);
// With else
bool isDarkMode = true;
var themeColors = [
if (isDarkMode) '#1a1a2e' else '#ffffff',
if (isDarkMode) '#16213e' else '#f5f5f5',
];
print(themeColors);
// Nested conditions
int age = 25;
bool isStudent = false;
var pricing = [
'Standard: \$10',
if (age < 18) 'Child Discount: \$5',
if (isStudent) 'Student Discount: \$7',
if (age >= 60) 'Senior Discount: \$6',
];
print(pricing);
}
Output:
[Home, Profile, Admin Panel, Upgrade to Premium]
{product: Dart Book, price: 29.99}
[#1a1a2e, #16213e]
[Standard: $10]
Explanation
if (condition) element includes element in the collection only when condition is true.
if (condition) elementA else elementB includes elementA when true, otherwise elementB.
- Multiple
if entries can appear in the same collection literal.
- Collection
if works in List, Set, and Map literals.
Notes
- The condition inside collection
if must be a boolean expression.
- You can only include one element per
if — use the spread operator with a sub-list if you need multiple conditional elements.
- Collection
if is evaluated at the time the collection literal is created.
Common Mistakes
- Trying to include multiple elements with a single
if — use if (cond) ...[a, b] instead.
- Forgetting that the else branch is optional — if omitted and the condition is false, nothing is added.
- Using collection
if when a where() filter or ?? would be more appropriate.
💪 Exercise
- Build a navigation menu list that includes a "Settings" item only if
isLoggedIn is true, and a "Login" item otherwise.
- Create a product map that includes a "discount" key only if a discount percentage is greater than 0.
- Build a list of tags that conditionally includes "featured" and "sale" based on boolean flags.
🧠 Quiz
1. What does collection if do when its condition is false and there is no else?
- A) Adds null
- B) Throws an error
- C) Adds nothing ✅
- D) Adds false
2. Which collection types support collection if?
- A) Only List
- B) List and Set only
- C) List, Set, and Map ✅
- D) None — it is not a real feature
Summary
Collection if allows you to conditionally include elements directly inside collection literals. It supports if and if-else forms and works with List, Set, and Map. To conditionally include multiple elements, combine collection if with the spread operator. This feature makes dynamic collection construction clean and expressive.