relational_ pattern_ operand_ type_ not_ assignable
The constant expression type '{0}' is not assignable to the parameter type '{1}' of the '{2}' operator.
Description
#The analyzer produces this diagnostic when the operand of a relational pattern has a type that isn't assignable to the parameter of the operator that will be invoked.
Example
#
The following code produces this diagnostic because the operand in the
relational pattern (0
) is an
int
, but the
>
operator defined in
C
expects an object of type
C
:
class C {
const C();
bool operator >(C other) => true;
}
void f(C c) {
switch (c) {
case > 0:
print('positive');
}
}
Common fixes
#If the switch is using the correct value, then change the case to compare the value to the right type of object:
class C {
const C();
bool operator >(C other) => true;
}
void f(C c) {
switch (c) {
case > const C():
print('positive');
}
}
If the switch is using the wrong value, then change the expression used to compute the value being matched:
class C {
const C();
bool operator >(C other) => true;
int get toInt => 0;
}
void f(C c) {
switch (c.toInt) {
case > 0:
print('positive');
}
}
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.