Skip to main content
The *= operator is the multiplication assignment operator. It is a compound assignment operator that multiplies the current value of the variable on the left-hand side (LHS) by the value of the expression on the right-hand side (RHS) and assigns the product back to the LHS variable.

Syntax

variable *= expression;

Operational Equivalence

The expression a *= b is semantically equivalent to:
a = a * b;
Note: In compound assignment, the LHS is evaluated only once. If the LHS involves a complex expression (such as an indexer list[i]), it is not re-evaluated during the assignment step.

Type Constraints

For the operation to be valid, the following type rules apply:
  1. Operator Support: The type of the LHS variable must define the multiplication operator (*).
  2. Assignability: The return type of the multiplication operation must be assignable to the static type of the LHS variable.

Examples

Integer Multiplication
int x = 5;
x *= 3; 
// x is now 15
Double Multiplication
double y = 2.5;
y *= 4; 
// y is now 10.0
Type Mismatch Error The following results in a compile-time error because multiplying an int by a double results in a double, which cannot be assigned to an int variable.
int z = 10;
z *= 2.5; // Error: A value of type 'double' can't be assigned to a variable of type 'int'.

Operator Overloading

If a custom class overloads the * operator, *= automatically utilizes that implementation.
class Vector {
  final int x, y;
  Vector(this.x, this.y);

  // Overloading the multiplication operator
  Vector operator *(int scalar) {
    return Vector(x * scalar, y * scalar);
  }
}

void main() {
  var v = Vector(1, 2);
  v *= 3; // Uses the overloaded * operator
  // v is now Vector(3, 6)
}
Master Dart with Deep Grasping Methodology!Learn More