Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The ...? (null-aware spread) operator evaluates an expression that yields a collection, and if that collection is not null, unpacks its elements into the surrounding collection literal. If the evaluated expression is null, the operator safely ignores it and performs no operation, preventing runtime null dereference exceptions.

Syntax

The operator is placed immediately preceding the nullable collection identifier or expression within a collection literal (List, Set, or Map).
// For Lists and Sets (operates on Iterables)
[elementA, ...?nullableIterable, elementB]
{elementA, ...?nullableIterable, elementB}

// For Maps (operates on Map entries)
{keyA: valueA, ...?nullableMap, keyB: valueB}

Evaluation Mechanics

When the Dart compiler encounters the ...? operator, it executes the following logical sequence:
  1. Expression Evaluation: It evaluates the operand to the right of the ...? operator.
  2. Null Check: It checks if the resulting value is null.
  3. Branching:
    • If null: The operator acts as a no-op. No elements are yielded to the surrounding collection literal, and execution proceeds to the next element in the literal.
    • If non-null: The operator behaves identically to the standard spread operator (...). It requests an Iterator from the collection and sequentially appends each element (or key-value pair, in the case of a Map) into the surrounding collection literal.

Code Visualization

List<int>? nullList;
List<int>? populatedList = [2, 3];

// The nullList is safely ignored.
List<int> resultA = [1, ...?nullList, 4]; 
// Evaluates to: [1, 4]

// The populatedList is unpacked.
List<int> resultB = [1, ...?populatedList, 4]; 
// Evaluates to: [1, 2, 3, 4]

Type System Interaction

Under Dart’s sound null safety, the standard spread operator (...) requires an operand with a strictly non-nullable type (e.g., List<T>). Attempting to use ... on a nullable type (e.g., List<T>?) results in a compile-time error. The ...? operator explicitly signals to the analyzer that the potential null state is handled. It accepts operands of type Iterable<T>? (for Lists/Sets) or Map<K, V>? (for Maps). The surrounding collection literal infers its type based on the generic type parameters of the unpacked collection, stripping away the nullability of the collection itself.
Master Dart with Deep Grasping Methodology!Learn More