TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
?[] (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 encountersexpression?[index], it executes the following logic:
- It evaluates the base
expressionexactly once. - If the result is
null, the operation immediately yieldsnull. It does not attempt to evaluate theindexexpression or invoke the underlying[]method. - If the result is a non-null object, it evaluates the
indexexpression and invokes the standard[](index) operator on that object.
?[] 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 expressioncollection?[index]resolves to typeT?. - 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: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.
Master Dart with Deep Grasping Methodology!Learn More





