?[]) conditionally accesses an element within a subscriptable object (such as a List or Map) only if that object is not null. If the receiver is null, the expression evaluates to null immediately; otherwise, it performs the standard index operation.
Syntax
Operational Semantics
When the expressione1?[e2] is executed:
- Receiver Evaluation: The expression
e1(the receiver) is evaluated. - Null Check:
- If
e1evaluates tonull, the operation short-circuits. The index expressione2is not evaluated, and the entire expression returnsnull. - If
e1is notnull,e2is evaluated, and the standard[]operator is invoked one1with the result ofe2.
- If
Logical Equivalence
The operator is syntactic sugar that ensures the receiver is evaluated exactly once. It is logically equivalent to assigning the receiver to a temporary variable before checking for null:Type Implications
The return type of a null-aware index operation is always nullable. The operator “lifts” the element type of the collection into a nullable type. Given a receiver of typeList<T>?:
- Direct Access:
list[0]results in a compile-time error because the receiver might be null. - Null-Aware Access:
list?[0]is valid and returns typeT?.
T), the result of the expression must be T? to account for the case where the receiver itself is null.
Implementation Example
Master Dart with Deep Grasping Methodology!Learn More





