Skip to main content
The /= operator is a compound assignment operator in Dart that performs division and assignment in a single operation. It divides the value of the left operand by the value of the right operand and assigns the resulting floating-point quotient back to the left operand.
a /= b;
This syntax is semantically equivalent to:
a = a / b;

Type Constraints and Behavior

In Dart, the standard division operator (/) strictly evaluates to a double, regardless of whether the operands are integers or floating-point numbers. Because the /= operator implicitly relies on /, it enforces specific type constraints on the left operand:
  1. Valid Types: The left operand must be declared as a double or the broader num type. If a num is initially assigned an integer, using /= will dynamically change its runtime type to double.
  2. Compile-Time Error: You cannot use the /= operator on a variable strictly typed as an int. Doing so results in a compile-time error: A value of type 'double' can't be assigned to a variable of type 'int'.

Syntax Visualization

// Valid: Left operand is a double
double a = 20.0;
a /= 4; 
print(a); // Output: 5.0

// Valid: Left operand is a num
num b = 10; // Runtime type is int
b /= 2;     // Runtime type becomes double
print(b);   // Output: 5.0

// Invalid: Left operand is strictly an int
int c = 10;
// c /= 2;  // ERROR: A value of type 'double' can't be assigned to a variable of type 'int'.
(Note: If integer division assignment is required, Dart provides the ~/= operator, which performs truncating division and allows the left operand to remain an int.)
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More