<) is a binary operator that compares a receiver (left-hand operand) against an argument (right-hand operand). While conventionally used to return a boolean indicating strict inequality, the operator is method-based and can be defined to return any type.
Syntax
Semantics
The expressiona < b is syntactic sugar for a method invocation. The behavior depends on the static type of the receiver a.
Static Resolution
Ifa is not dynamic, the compiler resolves the operator in the following order:
- Instance Member: The compiler looks for an instance method named
operator <defined on the static type ofaor inherited from a superclass. - Extension Member: If no instance member is found, the compiler looks for an applicable extension method that defines
operator <for the type ofa.
operator < (e.g., int), an extension method defining < on that same type will be ignored in favor of the instance member.
Dynamic Resolution
If the static type ofa is dynamic, the lookup occurs at runtime. The runtime system attempts to invoke the operator < method on the actual runtime type of the object. If the method does not exist or the argument type is incompatible, a NoSuchMethodError or TypeError is thrown.
Declaration
To define the< operator for a class or extension, use the operator keyword followed by the < symbol.
Type Constraints
- Receiver: The type of the left operand must define or inherit the
operator <method, or have an applicable extension method. - Argument: The right operand must be assignable to the parameter type defined in the method signature.
- Return Type: The Dart language specification permits any return type. However, to use the expression in boolean contexts (such as
ifstatements,assert, or logical&&operations), the operator must returnbool.
Defining Operator Logic
Custom types define comparison logic by implementing the operator as an instance method.Extension Methods
The< operator can be defined for types that do not natively implement it using extension methods. This applies only when the type does not already possess an instance method for <.
Null Safety
The< operator is not defined by default for nullable types (T?). The class Null does not define the < operator, and the Object class does not define it.
x! < 10) or checked explicitly (e.g., x != null && x < 10).
Master Dart with Deep Grasping Methodology!Learn More





