> ## 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.

# Dart Subtraction Assignment

The `-=` 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.

```dart theme={"dark"}
target -= expression;
```

While conceptually similar to `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 like `LHS -= RHS` strictly follows these steps:

1. 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). The `LHS` expression is evaluated exactly once.
2. Evaluate the right-hand side (`RHS`) expression.
3. Invoke the `-` operator on the previously read `LHS` value, passing the evaluated `RHS` value as the argument.
4. Assign the resulting value back to the `LHS` target (invoking the setter or index assignment if applicable).

Because the `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.

```dart theme={"dark"}
int x = 10;
x -= 3; // Valid: x becomes 7

double y = 10.5;
y -= 2; // Valid: y becomes 8.5 (int 2 is implicitly handled by double subtraction)

int z = 5;
// z -= 1.5; // Compile-time error: The result of int - double is double, which cannot be assigned to int.
```

**Operator Overloading**
Dart does not permit direct overriding of the `-=` 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.

```dart theme={"dark"}
class Vector {
  final int x;
  Vector(this.x);

  // Overriding the subtraction operator implicitly enables -=
  Vector operator -(Vector other) {
    return Vector(this.x - other.x);
  }
}

void main() {
  Vector v1 = Vector(10);
  Vector v2 = Vector(3);
  
  v1 -= v2; // Valid: Reads v1, evaluates v2, subtracts, and reassigns the new Vector to v1
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Dart Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
