+= operator is a compound assignment operator that performs addition (for numbers) or concatenation (for strings) using the right operand, and assigns the result to the left operand. It serves as syntactic sugar for applying the + operator and the = operator in a single statement.
Syntax
Operational Semantics
The expressiona += b is semantically equivalent to a = a + b, with the distinction that the left-hand operand (a) is evaluated only once.
The execution flow is as follows:
- Evaluation: The current value of
variableis retrieved. - Operation: The
+operator is invoked onvariablewithexpressionas the argument. - Assignment: The result of the operation is assigned back to
variable.
Type Compatibility
The behavior of+= depends on the static type of the left-hand operand:
- Numeric Types (
int,double): Performs arithmetic addition. - String Type (
String): Performs string concatenation. - Custom Types: Invokes the user-defined
operator +method on the class.
Operator Overloading
Dart does not permit the explicit overloading of the+= operator. Instead, the behavior of += is derived implicitly from the implementation of the + operator. To enable += for a custom class, you must define operator +.
Examples
Numeric Additioni is evaluated only once, ensuring the index does not increment twice.
Master Dart with Deep Grasping Methodology!Learn More





