Skip to main content
The -- operator is a unary arithmetic decrement operator in Dart that subtracts exactly 1 from its numeric operand. It mutates the underlying variable in place and requires the operand to be a non-final, non-const variable. The operator functions in two distinct evaluation contexts based on its placement relative to the operand: prefix and postfix.

Prefix Decrement (--var)

In the prefix position, the operator performs a pre-decrement. Dart subtracts 1 from the operand before evaluating the expression. The expression resolves to the newly decremented value.

Postfix Decrement (var--)

In the postfix position, the operator performs a post-decrement. Dart evaluates the expression using the current value of the operand, and subtracts 1 from the operand after the evaluation is complete.

Technical Constraints and Behavior

  • Type Requirements: The operand must be of type num (int or double), or a custom class that implements the - operator.
  • Immutability: Applying -- to a final or const variable results in a compile-time error, as the operator fundamentally requires reassignment.
  • Desugaring: Dart does not allow direct overriding of the -- operator. Instead, the Dart compiler treats a-- or --a as syntactic sugar for a = a - 1.
  • Custom Classes: Because of the desugaring mechanism, if you want to use the -- operator on an instance of a custom class, you must override the binary - operator. The class must also be assigned to a mutable variable (var or explicit type, not final).
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More