Skip to main content
The ?[] (null-aware index) operator conditionally evaluates access to a collection’s elements. It returns the value at the specified index or key if the collection evaluates to a non-null object, and short-circuits to return null if the collection reference evaluates to null.

Evaluation Mechanics

When the Dart compiler encounters expression?[index], it executes the following logic:
  1. It evaluates the base expression exactly once.
  2. If the result is null, the operation immediately yields null. It does not attempt to evaluate the index expression or invoke the underlying [] method.
  3. If the result is a non-null object, it evaluates the index expression and invokes the standard [] (index) operator on that object.
Because the ?[] operator evaluates the base expression exactly once, it is semantically distinct from a naive ternary operation (expression != null ? expression[index] : null), which evaluates the base expression twice. The single-evaluation guarantee of ?[] prevents redundant computations or unintended side effects when the base expression is a method call. Its behavior is accurately represented by binding the evaluated base expression to a temporary variable:

Type Resolution

The return type of a ?[] operation is inherently forced to be nullable, regardless of the collection’s generic type arguments.
  • If a collection is typed as List<T>?, the expression collection?[index] resolves to type T?.
  • Even if the collection contains non-nullable elements (e.g., List<int>?), the result of the null-aware index operation must be assigned to a nullable variable (e.g., int?).

Syntax Examples

List Indexing:
Map Key Access:

Null-Aware Index Assignment

Dart allows a null-aware index expression (?[]) to be used as an assignable target (l-value) in conjunction with the standard assignment operator (=). There is no distinct ?[]= operator token; rather, the language permits the null-aware index syntax on the left side of an assignment. This allows mutation of a collection at a specific index only if the collection is not null. If the base collection evaluates to null, the entire assignment operation short-circuits—the assignment is silently ignored, and the right-hand side expression is never evaluated.
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More