Skip to main content
The += (addition assignment) operator is a compound assignment operator that evaluates the right-hand expression, adds its value to the current value of the left-hand variable, and assigns the computed result back to the left-hand variable.

Syntax

variable += expression;
This operation is syntactic sugar for the following standard assignment:
variable = variable + expression;

Type-Specific Behavior

The underlying operation executed by += is dictated by the static type of the left operand and its specific implementation of the + operator:
  • Numeric Types (int, double, num): Performs standard arithmetic addition.
  • Strings (String): Performs string concatenation.
  • Custom Classes: Invokes the overridden operator + method defined on the left operand’s class.

Technical Characteristics

  • Single Evaluation: The left operand is evaluated exactly once. In a complex expression such as collection[computeIndex()] += value, the computeIndex() function is executed only one time. This differs from the expanded form collection[computeIndex()] = collection[computeIndex()] + value, which would evaluate the index twice.
  • Type Compatibility: The return type of the underlying + operation must be assignable to the static type of the left operand. For example, if a is an int and b is a double, the expression a += b will throw a compile-time error. The operation a + b yields a double, which cannot be assigned back to the int variable a without an explicit cast.
  • Mutability Requirement: The left operand must be a mutable reference. It cannot be a variable declared with the final or const modifiers.
  • Null Safety: Neither the left nor the right operand can be null unless the left operand’s class explicitly overrides operator + to accept nullable types (which is highly non-standard in Dart). Attempting += on a nullable numeric or string type without prior null-checking will result in a compile-time error.
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More