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 assignment operator that increments an assignable expression by invoking its + operator with the exact integer argument 1. Because Dart variables and properties hold references and numeric types are immutable, this operator does not mutate the underlying object in place; instead, it reassigns the expression to reference the newly created object resulting from the addition. The operator requires the operand to be an assignable expression. This includes non-final local variables, property accesses (e.g., object.property), and index expressions (e.g., list[index]). Because Dart supports operator overloading, ++ is not restricted to numeric types. It can be applied to any custom class that defines operator +, provided the return type of that method is assignable back to the expression’s declared type. Depending on its placement relative to the operand, the operator exhibits two distinct evaluation semantics: prefix and postfix. Prefix Increment (++expr) In the prefix form, the expression evaluates the addition, performs the reassignment, and then yields the newly assigned value.
int a = 10;
int b = ++a; 

// Evaluation sequence:
// 1. The expression '++a' evaluates 'a + 1' (11).
// 2. 'a' is reassigned to reference the new object (11).
// 3. The expression '++a' yields 11.
// 4. 'b' is assigned the yielded value (11).
// Final state: a = 11, b = 11
Postfix Increment (expr++) In the postfix form, the expression yields the original value of the operand, but the reassignment occurs during the evaluation of the expr++ expression itself. The increment side-effect completes entirely before the yielded value is used in any surrounding context, such as an outer assignment.
int x = 10;
int y = x++; 

// Evaluation sequence:
// 1. The expression 'x++' caches the current value of 'x' (10).
// 2. 'x' is reassigned to reference the result of 'x + 1' (11).
// 3. The expression 'x++' completes and yields the cached value (10).
// 4. 'y' is assigned the yielded value (10).
// Final state: x = 11, y = 10
Technical Constraints and Behavior
  • Single Evaluation: While conceptually similar to expr = expr + 1, the ++ operator guarantees that the receiver or index of the assignable expression is evaluated exactly once. For example, in the index expression list[getIndex()]++, the function getIndex() is called only once. The expanded form list[getIndex()] = list[getIndex()] + 1 would evaluate the index twice, potentially causing unintended side effects.
  • Assignable Expression Requirement: The operator must be applied to a valid assignable expression. Applying it to a literal value (e.g., 5++) or an unassignable expression (such as a final variable or a getter without a corresponding setter) results in a compile-time error.
  • Type Resolution: The operation does not inherently preserve the operand’s type. The resulting type depends entirely on the return type defined by the object’s operator + method. The Dart analyzer enforces that this return type must be assignable to the expression’s declared type to guarantee type safety during the implicit reassignment.
  • Integer Argument: According to the Dart language specification, the operator always invokes the + operator with the integer 1, regardless of the operand’s type (e.g., if applied to a double, it evaluates as expr + 1, not expr + 1.0).
Master Dart with Deep Grasping Methodology!Learn More