relational_ pattern_ operator_ return_ type_ not_ assignable_ to_ bool
The return type of operators used in relational patterns must be assignable to 'bool'.
Description
#
The analyzer produces this diagnostic when a relational pattern references
an operator that doesn't produce a value of type bool.
Example
#
The following code produces this diagnostic because the operator >, used
in the relational pattern > c2, returns a value of type int
rather
than a bool:
class C {
const C();
int operator >(C c) => 3;
bool operator <(C c) => false;
}
const C c2 = C();
void f(C c1) {
if (c1 case > c2) {}
}
Common fixes
#If there's a different operator that should be used, then change the operator:
class C {
const C();
int operator >(C c) => 3;
bool operator <(C c) => false;
}
const C c2 = C();
void f(C c1) {
if (c1 case < c2) {}
}
If the operator is expected to return bool, then update the declaration
of the operator:
class C {
const C();
bool operator >(C c) => true;
bool operator <(C c) => false;
}
const C c2 = C();
void f(C c1) {
if (c1 case > c2) {}
}
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Page last updated on 2025-9-4. View source or report an issue.