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');
}
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。