Skip to main content
The %= operator is a compound assignment operator in Dart, formally known as the modulo assignment operator. It performs a modulo operation using the left-hand and right-hand operands, and assigns the resulting value back to the left-hand operand.
This syntax is semantically equivalent to the expanded assignment, with the technical distinction that the left-hand target is evaluated only once:
Technical Characteristics:
  • Evaluation Order: Dart evaluates the left-hand expression first to determine the exact assignment target (such as resolving an object reference, property getter, or array index). It then evaluates the right-hand expression, performs the modulo operation against the target’s current value, and finally executes the assignment.
  • Type Constraints: The left-hand operand must be a mutable variable or a settable property (cannot be const or final after initialization). While natively supported by Dart’s built-in numeric types (int, double, num), the %= operator is not restricted to numbers; it can be applied to any custom class that explicitly overrides operator %.
  • Euclidean Modulo (Numeric Types): For Dart’s core numeric types, the underlying % operator implements Euclidean division. Consequently, the assigned remainder will always be a non-negative value, regardless of whether the left-hand or right-hand operand is negative.
  • Division by Zero (Numeric Types): If the right-hand operand evaluates to 0, the operation yields NaN (Not-a-Number) if the left-hand variable is a double, or throws an UnsupportedError if the variable is an int.
Execution Example:
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More