...?) allows for the expansion of a nullable collection expression into a target collection literal. It functions identically to the standard spread operator (...) when the operand is non-null, but safely omits the operation if the operand evaluates to null, preventing a runtime exception.
Syntax
The operator is used as a prefix within collection literals (Lists, Sets, and Maps).Operational Logic
When the Dart runtime encounters...?expression:
- Evaluation: The
expressionto the right of the operator is evaluated. - Null Check:
- If the result is
null: The operator does nothing. No elements are added to the target collection, and execution continues to the next element in the literal. - If the result is non-null: The operator behaves exactly like the standard spread operator (
...), iterating over the collection and inserting its individual elements (or key-value pairs) into the target.
- If the result is
Code Demonstration
The following example demonstrates the operator’s behavior with both null and non-null references in List and Map contexts.Type Constraints
- Iterables: When used in a List or Set literal, the expression must evaluate to a type assignable to
Iterable<T>?, whereTis compatible with the target collection’s element type. - Maps: When used in a Map literal, the expression must evaluate to a type assignable to
Map<K, V>?, whereKandVare compatible with the target map’s key and value types.
Master Dart with Deep Grasping Methodology!Learn More





