A relational pattern compares a matched value against a given constant using standard relational operators. It evaluates toDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
true if the boolean result of applying the operator between the matched value and the constant expression is true.
Syntax
<relational_operator> can be any of the following: ==, !=, <, >, <=, or >=.
The <constant_expression> must evaluate to a compile-time constant.
Mechanics and Behavior
- Constant Requirement: The operand on the right side of the relational operator must be a compile-time constant. Variables evaluated at runtime are not permitted.
- Static Type Safety: The Dart analyzer statically verifies that the relational operator is defined for the static type of the matched value. If the matched value is of a type that does not implement the specified operator, a compile-time error occurs.
- Evaluation Order: When matching a value
vagainst a relational patternop c, the expression evaluates exactly asv op c. - Exhaustiveness Checking: Dart’s exhaustiveness checker does not reason about the mathematical completeness of relational patterns. Even if a set of relational patterns logically covers all possible values (e.g.,
< 0,== 0,> 0), the compiler cannot deduce this. A default case (such as_) is strictly required in switch expressions utilizing relational patterns to prevent anon_exhaustive_switch_expressioncompile-time error.
Code Examples
In a Switch Expression:&&) or logical OR (||) patterns to define bounded ranges.
if-case Statement:
Operator Overloading
Relational patterns respect custom operator overloading. If a user-defined class overrides a relational operator (such asoperator >), a relational pattern can be applied to instances of that class, provided the right-hand operand is a valid constant of the expected type.
Master Dart with Deep Grasping Methodology!Learn More





