= operator performs simple assignment, evaluating the expression on the right-hand side (RHS) and assigning the resulting object reference to the l-value on the left-hand side (LHS). Depending on the target l-value, this operation either updates a variable’s storage or invokes a specific mutator method.
Syntax
Valid L-Values
An l-value is an expression that identifies a specific storage location or mutator. In Dart, valid l-values are classified as:- Variables: Identifiers for direct storage, including local variables, parameters, top-level variables, and static variables (fields).
- Properties: Accessors that resolve to setter methods. This includes instance variables (implicit setters), explicit instance setters, and static setters.
- Subscript Expressions: Elements accessed via the subscript operator
[].
Operational Semantics
The execution of an assignment proceeds as follows:- RHS Evaluation: The expression on the right is evaluated to produce a value (an object reference).
- Assignment Mechanism:
- Variable Update: If the LHS is a variable, the operator updates the variable to hold the reference to the new object.
- Setter Invocation: If the LHS is a property, the operator invokes the corresponding setter method (implicit or explicit), passing the RHS value as the argument.
- Subscript Operator Invocation: If the LHS is a subscript expression (e.g.,
list[i]), the operator invokes theoperator []=method on the target object.
- Return Value: The assignment expression resolves to the value of the RHS. This allows the assignment to be used as an expression within larger statements.
Type Safety Rules
Dart enforces static type checking on assignments. For an assignment to be valid, the static type of the RHS expression must be a subtype of the static type of the LHS. Since the introduction of sound null safety, implicit downcasts are prohibited for non-dynamic types. A supertype cannot be assigned to a subtype without an explicit cast.
Associativity
The= operator is right-associative. When multiple assignments are chained, they are evaluated from right to left.
Code Representation
Variable Assignment Updates the reference held byx.
operator []= method on the collection instance.
Immutability Constraints
The= operator cannot be used to reassign identifiers marked as final or const once they have been initialized.
Master Dart with Deep Grasping Methodology!Learn More





