*= operator is a compound assignment operator that multiplies the current value of the left operand by the value of the right operand, and subsequently assigns the resulting product back to the left operand.
Syntax
Technical Mechanics
- Evaluation Order: Dart evaluates expressions strictly left-to-right. The left operand (the receiver/target) is evaluated first, followed by the right operand. The multiplication is then performed, and the result is assigned back to the left operand.
- Single Evaluation: The compound assignment operator evaluates the
leftOperandexactly once. This makesleftOperand *= rightOperandtechnically distinct from the expanded formleftOperand = leftOperand * rightOperandwhen the left operand contains side effects. For example,list[i++] *= 2incrementsionly once, whereaslist[i++] = list[i++] * 2would evaluate the left operand twice, incrementingitwice. - Mutability: The left operand must be a mutable reference. It cannot be declared as
constorfinal. - Operator Overloading: The
*=operator is not restricted to core numeric types. It functions by invoking theoperator *method on the left operand. It can be used with any custom class that implementsoperator *.
Type Safety and Promotion Rules
Because Dart is statically typed, the result of the multiplication operation must be assignable to the declared type of the left operand. Numeric Types When using Dart’s core numeric types (int, double, num), type promotion dictates the following behaviors:
- Integer Assignment: If both operands are integers, the result is an integer and safely assigns back to an
intvariable. - Double Assignment: If the left operand is a
double, the right operand can be either anintor adouble. The result will always be adouble. - Compile-Time Type Errors: If the left operand is strictly typed as an
intand the right operand is adouble, the multiplication yields adouble. Dart will not implicitly downcast adoubleto anint, resulting in a compile-time error.
*= with custom classes, the return type of the overloaded operator * must be assignable to the variable’s type, and the right operand must match the parameter type defined in the method signature.
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More





