-= operator is a compound assignment operator in Dart, specifically known as the subtraction assignment operator. It subtracts the value of the right-hand expression from the current value of the left-hand assignable expression and assigns the resulting value back to that left-hand target.
target = target - expression, the -= operator evaluates the left-hand side exactly once. This distinction is critical when the left operand is a complex expression with side effects. For example, list[i++] -= 1 evaluates the index i only once, whereas list[i++] = list[i++] - 1 evaluates i twice, leading to divergent logical outcomes.
Technical Characteristics
Evaluation Order Dart enforces strict left-to-right evaluation. The execution sequence for a compound assignment likeLHS -= RHS strictly follows these steps:
- Evaluate the left-hand side (
LHS) to determine the target (e.g., a variable binding, property access, or index expression), and immediately read its current value (invoking the getter or indexer if applicable). TheLHSexpression is evaluated exactly once. - Evaluate the right-hand side (
RHS) expression. - Invoke the
-operator on the previously readLHSvalue, passing the evaluatedRHSvalue as the argument. - Assign the resulting value back to the
LHStarget (invoking the setter or index assignment if applicable).
LHS value is read prior to the RHS evaluation, any state changes triggered by the RHS expression will not affect the initial value used for the subtraction.
Mutability Requirements
The left operand must be an assignable expression. This includes mutable local variables, class properties with setters, and collection index expressions. Attempting to use -= on a target that cannot be reassigned—such as a variable declared as final or const, or a getter-only property—results in a compile-time error. In Dart, variables and properties act as bindings that hold references to objects. The assignment phase of the -= operator requires that the target binding can be updated to point to the newly computed reference.
Type Safety and Casting
Dart’s static typing rules apply strictly to the assignment phase of the -= operator. The result of the subtraction operation must be assignable to the declared static type of the left operand.
-= operator. Instead, -= is automatically derived from the - operator. To use -= with instances of a custom class, the class must override operator -. The return type of the overridden operator - must be compatible with the static type of the left operand to satisfy assignment rules.
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More





