Collection literals can express a small assembly step without repeated add calls. Keep that step readable: conditions should describe whether an element belongs in the result, while validation should happen before the literal becomes too complicated.
Worked example
Language requirement: Dart 3.8 or later. This complete example uses browser-supported libraries.
List<String>? extrasFor(bool priority) => priority ? ['priority support'] : null;
void main() {
final String? coupon = null;
final extras = extrasFor(true);
final includeReceipt = true;
final options = <String>[
'standard delivery',
if (includeReceipt) 'email receipt',
?coupon,
...?extras,
for (final day in ['Monday', 'Tuesday']) 'Slot: $day',
];
print(options.join(' | '));
}Follow the data and control flow
The if includes one element when the condition is true. ?coupon omits a null element; ...?extras spreads zero elements when the entire list is null. The final for produces one label for each day. These are collection elements, not a general replacement for null checks throughout the language.
Null-aware collection elements require Dart 3.8 or later. Omitting a null value differs from keeping an explicit null in a request payload: an API may interpret those differently. Keep the wire contract visible when constructing JSON.
A mistake to avoid
A null-aware element does not validate a non-null string. An empty coupon still becomes an empty element. Also avoid deeply nesting several for and if elements when a named helper would communicate the rule more clearly.
Practice and verification
Set coupon to SAVE10, then set extras to null. Predict each result before running. Build a separate JSON map where an absent optional field is omitted, and explain when you would instead intentionally send that field as null.