Skip to main content
The < (less than) operator is a relational binary operator that evaluates whether the left operand is strictly smaller in value than the right operand, returning a bool (true or false). In Dart, operators are implemented as instance methods. When the < operator is evaluated, Dart invokes the operator < method defined on the left operand’s class, passing the right operand as the argument.
bool result = leftOperand < rightOperand;

Built-in Type Behavior

For built-in numeric types (num, int, double), the < operator performs standard mathematical comparison. The Dart type system safely handles cross-type numeric comparisons (e.g., comparing an int to a double).
bool isLessNum = 5 < 10;        // true
bool isLessMixed = 5.0 < 5;     // false (strictly less than)
bool isLessNegative = -2 < -1;  // true
Note: Dart’s String class does not define relational operators. Attempting to use < on strings will result in a compile-time error. To compare strings lexicographically, developers must use the compareTo method.

Operator Overloading

Because < is an instance method, it can be defined or overridden in custom classes to establish specific comparison semantics for objects. The method must return a bool and typically accepts a parameter of the same class type.
class Temperature {
  final double celsius;

  const Temperature(this.celsius);

  // Overriding the < operator
  bool operator <(Temperature other) {
    return this.celsius < other.celsius;
  }
}

void main() {
  final t1 = Temperature(20.5);
  final t2 = Temperature(30.0);

  bool result = t1 < t2; // Evaluates to true
}

Type Safety and Constraints

When defining the < operator, Dart’s static analyzer enforces the parameter type declared in the method signature. If the right operand does not match the expected type, the compiler throws an error. For built-in types like num, the parameter is typed as num, preventing direct comparison with incompatible types like String or bool.
Tired of Poor Dart Skills? Fix That With Deep Grasping!Learn More