Collection Literals
Collection If
Conditional Element
An if (cond) value inside a literal includes the value only when the condition is true.
Program
Play the program to include a bonus element when a flag is on.
collection_if.dart
Replay: real traced execution (multi-file project)
void main() {
var includeBonus = true;
var items = ['a', 'b', if (includeBonus) 'bonus'];
print(items);
}
includeBonus ← true
1void main() {2 var includeBonus = true;3 var items = ['a', 'b', if (includeBonus) 'bonus'];values this steptrueincludeBonusitems ← [a, b, bonus]
2var includeBonus = true;3var items = ['a', 'b', if (includeBonus) 'bonus'];4print(items);values this step[a, b, bonus]itemstrueincludeBonusprint(items);
3 var items = ['a', 'b', if (includeBonus) 'bonus'];4 print(items);5}output[a, b, bonus]values this step[a, b, bonus]items
collection-if
`if (cond) value` lives inside a list/set/map literal.
inline branching
Avoids building the list and patching it later.
clarity
Reads like the data layout itself.