Skip to main content
The + operator is a binary additive operator used to perform arithmetic addition on numeric operands or concatenation on string operands. In Dart, operators are instance methods with special syntax; the expression a + b is syntactic sugar for the method invocation a.+(b).

Syntax

operand1 + operand2

Underlying Mechanism

Because Dart treats operators as instance methods, the behavior of + is defined by the class of the left-hand operand. The Dart compiler resolves the operation by looking up the operator + method signature on the type of operand1.

Standard Implementations

Numeric Addition

The num class and its subtypes, int and double, implement operator + to perform arithmetic addition.
  • int + int: Returns an int.
  • double + double: Returns a double.
  • Mixed types: If one operand is a double, the result is promoted to a double.
int a = 10;
int b = 5;
double c = 2.5;

var sumInt = a + b;    // Result: 15 (int)
var sumDouble = a + c; // Result: 12.5 (double)

String Concatenation

The String class implements operator + to perform concatenation. This operation returns a new String object containing the sequence of code units from the left operand followed immediately by the code units from the right operand.
String s1 = "Hello";
String s2 = " World";

String result = s1 + s2; // Result: "Hello World"

Operator Overloading

Custom classes can define or override the + operator by implementing the operator + method. The method must accept a single argument (the right-hand operand) and return a result. The following example demonstrates overloading + for vector addition:
class Vector {
  final int x;
  final int y;

  const Vector(this.x, this.y);

  // Definition of the + operator
  Vector operator +(Vector other) {
    return Vector(x + other.x, y + other.y);
  }
}

void main() {
  final v1 = Vector(1, 2);
  final v2 = Vector(3, 4);
  final v3 = v1 + v2; // Invokes v1.+(v2)
}

Precedence and Associativity

The + operator has additive precedence, which is lower than multiplicative operators (*, /, %) but higher than bitwise shift operators (<<, >>). It is left-associative, meaning expressions are evaluated from left to right.
// Equivalent to (1 + 2) + 3
var result = 1 + 2 + 3; 

// Equivalent to 1 + (2 * 3) due to precedence
var mixed = 1 + 2 * 3; 
Master Dart with Deep Grasping Methodology!Learn More