+= (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
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, thecomputeIndex()function is executed only one time. This differs from the expanded formcollection[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, ifais anintandbis adouble, the expressiona += bwill throw a compile-time error. The operationa + byields adouble, which cannot be assigned back to theintvariableawithout an explicit cast. - Mutability Requirement: The left operand must be a mutable reference. It cannot be a variable declared with the
finalorconstmodifiers. - Null Safety: Neither the left nor the right operand can be
nullunless the left operand’s class explicitly overridesoperator +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





