Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The -- operator is a unary arithmetic decrement operator that evaluates its operand minus 1, yields a new object representing that value, and updates the operand’s reference to point to this new object. Because numbers in Dart are immutable, this operator does not mutate the existing value in memory; instead, it performs a reassignment. It requires a mutable variable (non-final and non-const) and functions in two distinct evaluation modes based on its syntactic position: prefix and postfix. Prefix Decrement (--var) When the operator precedes the operand, Dart executes a pre-decrement operation. At runtime, the expression evaluates the operand minus 1, updates the variable’s reference to the newly created object, and then yields this new value as the result of the expression.
int prefixVar = 10;
int result = --prefixVar; 

// Runtime execution order:
// 1. Evaluates prefixVar - 1 (yields a new integer object, 9)
// 2. Updates the prefixVar reference to point to 9
// 3. result is assigned the new value (9)
Postfix Decrement (var--) When the operator follows the operand, Dart executes a post-decrement operation. At runtime, the expression caches the variable’s current object reference to yield as the final result, evaluates the operand minus 1 to create a new object, and updates the variable’s reference to this new object.
int postfixVar = 10;
int result = postfixVar--; 

// Runtime execution order:
// 1. Caches the current reference of postfixVar (10)
// 2. Evaluates postfixVar - 1 (yields a new integer object, 9)
// 3. Updates the postfixVar reference to point to 9
// 4. result is assigned the cached reference (10)
Underlying Mechanics While conceptually similar to the assignment expression var = var - 1, the -- operator provides a strict single-evaluation guarantee. For complex assignable expressions (l-values) like list[getIndex()]-- or getObj().prop--, Dart evaluates the receiver and any index expressions exactly once at runtime. A literal var = var - 1 expansion would incorrectly evaluate those components twice. To use the -- operator, the following conditions must be met:
  1. The operand must be an assignable expression (a mutable l-value).
  2. The operand’s type must implement the subtraction operator (operator -) accepting an int as the right-hand operand.
  3. The return type of that operator - must be assignable back to the original variable’s static type to satisfy the implicit assignment step.
While natively applied to int and double types, the -- operator functions on custom class instances provided the class explicitly overrides operator - and adheres to the type assignability requirement.
Master Dart with Deep Grasping Methodology!Learn More