Skip to main content
The > (greater than) operator is a binary relational operator that evaluates whether the value of the left-hand operand is strictly greater than the value of the right-hand operand. It returns a bool result: true if the condition is met, and false otherwise.

Syntax

bool result = leftOperand > rightOperand;

Underlying Mechanism

In Dart, operators are instance methods with special syntax. The expression a > b is syntactic sugar that resolves to the invocation of the instance method defined as operator > on the left operand a, passing b as the argument. While the syntax a > b looks like a primitive operation, it strictly adheres to method dispatch rules, meaning the behavior is determined entirely by the class definition of the left-hand operand.

Standard Implementation

The core num type (and its subtypes int and double) implements this operator to perform numeric comparison.
int a = 10;
int b = 5;

// Evaluates to true because 10 is strictly greater than 5
bool isGreater = a > b; 

// Evaluates to false because 5 is not greater than 10
bool isLess = b > a;    

// Evaluates to false because 10 is not strictly greater than 10
bool isEqual = a > 10;  

Operator Overloading

Custom classes can define the behavior of the > operator by implementing the operator > method. The method must accept one argument (the right-hand operand) and conventionally returns a bool.
class Vector {
  final int x;
  final int y;

  Vector(this.x, this.y);

  // Calculates magnitude squared
  int get magnitudeSquared => (x * x) + (y * y);

  // Declaration of the > operator
  bool operator >(Vector other) {
    return this.magnitudeSquared > other.magnitudeSquared;
  }
}

void main() {
  final v1 = Vector(3, 4); // Magnitude squared: 25
  final v2 = Vector(1, 1); // Magnitude squared: 2

  // Internally calls v1's operator > method with v2 as the argument
  print(v1 > v2); // Output: true
}

Constraints

  • Null Safety: Neither operand can be null unless the operator implementation explicitly accepts a nullable type. Attempting to use > on a nullable receiver without a null check will cause a compile-time error.
  • Type Compatibility: The right-hand operand must be of a type accepted by the left-hand operand’s operator > method signature. Mismatched types will result in a static analysis error or a runtime TypeError.
  • Associativity: The > operator is non-associative. Expressions like a > b > c are invalid because a > b evaluates to a bool, and bool does not define a > operator.
Master Dart with Deep Grasping Methodology!Learn More