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 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.
expression?[index]

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:
// Semantic equivalent of expression?[index]
final temp = expression;
temp == null ? null : temp[index];

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:
List<int>? nullableList = null;
int? result1 = nullableList?[0]; // Evaluates to null

nullableList = [10, 20, 30];
int? result2 = nullableList?[0]; // Evaluates to 10
Map Key Access:
Map<String, double>? nullableMap = null;
double? value1 = nullableMap?['pi']; // Evaluates to null

nullableMap = {'pi': 3.14159};
double? value2 = nullableMap?['pi']; // Evaluates to 3.14159

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.
List<int>? numbers = null;
numbers?[0] = 42; // Operation short-circuits; numbers remains null

numbers = [1, 2, 3];
numbers?[0] = 42; // numbers is now [42, 2, 3]
Master Dart with Deep Grasping Methodology!Learn More