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 in Dart is a unary operator that increments the value of an assignable expression by one. Because Dart numbers are immutable, the operator does not mutate the object in memory. Instead, it evaluates the addition and reassigns (rebinds) the reference to point to the newly created value. It exists in two distinct syntactic forms—prefix and postfix—which dictate whether the expression yields the original or the incremented value. Under the hood, the ++ operator acts as syntactic sugar for the assignment x = x + 1. Prefix Increment (++var) The prefix form increments the operand and completes the reassignment before yielding the result. The expression resolves to the newly incremented value.
int prefixVar = 10;
int result = ++prefixVar; 

// Evaluation sequence:
// 1. The right-hand expression (++prefixVar) is evaluated:
//    a. prefixVar is incremented and reassigned to 11.
//    b. The expression yields the new value (11).
// 2. result is assigned the yielded value (11).
Postfix Increment (var++) The postfix form yields the operand’s original value before completing the increment and reassignment. The expression resolves to the un-incremented value, even though the underlying variable has been updated.
int postfixVar = 10;
int result = postfixVar++; 

// Evaluation sequence:
// 1. The right-hand expression (postfixVar++) is evaluated:
//    a. The original value of postfixVar (10) is cached.
//    b. postfixVar is incremented and reassigned to 11.
//    c. The expression yields the cached original value (10).
// 2. result is assigned the yielded value (10).
Technical Constraints and Behavior
  • Assignable Expressions: The operand must be a valid assignable expression. This includes standalone mutable variable identifiers, property accesses (e.g., myObject.value++), and index expressions (e.g., myList[0]++). The operator cannot be applied to literals (e.g., 5++ throws a compilation error), final variables, or const variables.
  • Type Requirements and Overloading: The operand is not restricted to built-in numeric types (int or double). Because the operation x++ or ++x is evaluated as x = x + 1, the operator can be applied to any custom class or type that implements operator + (accepting an int or num argument), provided the return type of that operator can be assigned back to the original assignable expression. When applied to a built-in double, the operator effectively adds 1.0 to the floating-point value.
  • Underlying Equivalence: While functionally equivalent to the standard assignment x = x + 1 or the compound assignment x += 1, the ++ operator is parsed and evaluated as a single unary expression.
Master Dart with Deep Grasping Methodology!Learn More