TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
~/= operator is a compound assignment operator that performs truncating division on the left operand by the right operand, assigning the resulting value back to the left operand. It combines the truncating division operator (~/) with the standard assignment operator (=), ensuring that the left operand is evaluated exactly once.
leftOperand = leftOperand ~/ rightOperand, the ~/= operator evaluates leftOperand only a single time. This distinction is critical when the left operand is an expression with side effects (e.g., list[i++] ~/= 2). In the compound assignment, the side effect (i++) occurs exactly once, whereas the expanded form would trigger it twice.
Operator Overloading and Type Constraints
The exact behavior and return type of the~/= operation depend on how the ~/ operator is defined for the left operand’s type:
- Built-in
numTypes (intanddouble): For numeric types,~/divides the two numbers, discards any fractional part of the quotient, and strictly returns anint. Consequently, theleftOperandmust be an assignable expression (an lvalue) of a type that accepts anintassignment (e.g.,int,num, ordynamic). If the left operand is explicitly typed as adouble, the operation triggers a compile-time error because Dart’s static type system prohibits assigning anintto adouble. - Other Core Types (
BigInt): TheBigIntclass implementsoperator ~/to return aBigInt. Therefore, when using~/=withBigIntoperands, the result is aBigInt, and the left operand must accept aBigIntassignment. - Custom Classes: Dart supports operator overloading. Custom classes can define
operator ~/to return any arbitrary type. The only static constraint is that the left operand must be of a type that accepts the specific return type of that overloaded operator.
Runtime Exceptions (Built-in num Types)
When operating on built-in num types, if the rightOperand evaluates to 0, the operation throws an UnsupportedError. The specific exception message depends on the platform and the runtime type of the left operand:
- Dart Native (VM/AOT): If the left operand holds an
intvalue, integer division by zero throws anUnsupportedErrorspecifically indicating “Integer division by zero”. - Dart Web (JavaScript Compilation): On the web, Dart integers are backed by JavaScript double-precision floating-point numbers. Consequently,
intdivision by zero (e.g.,10 ~/ 0) evaluates toInfinity.truncate(). This throws anUnsupportedErrorindicating “Infinity or NaN toInt”. - Floating-Point Operands: If the left operand holds a
doublevalue at runtime (but is statically typed asnumordynamicto allow the assignment), the intermediate division evaluates toInfinity(orNaN). Dart then attempts to truncate this result to an integer, throwing anUnsupportedErrorindicating “Infinity or NaN toInt” across all platforms.
Execution Examples
Master Dart with Deep Grasping Methodology!Learn More





