I have some simple code like this:
enum myEnum {
foo,
bar,
baz
}
mixin myMixin {
final Map<myEnum, double> values = {
myEnum.foo: 0.3,
myEnum.bar: 5.2,
myEnum.ter: 91.3,
};
double getValue(myEnum key, { double a = 0.0, double b = 0.0 }) {
final double tot = a b;
double c = 0.0;
switch(key) {
case myEnum.foo | myEnum.bar:
-1;
break;
}
return values[key]!;
}
}
and it is generating this error:
In constant expressions, operands of this operator must be of type 'bool' or 'int'.
in the line case myEnum.foo | myEnum.bar:
but I cannot get the reason for it.
Any idea?
CodePudding user response:
You're not writing correct Dart.
The way to match two different values in a switch is to have two case
clauses:
case myEnum.foo:
case myEnum.bar:
...
The |
operator is not defined on enums (usually, with enhanced enums you can declare one, it's a user-definable operator), and since the expression after case
must be a constant expression, and |
in constant expressions is only allowed for integers (bitwise or) and Booleans (Boolean or), the expression myEnum.foo | myEnum.bar
is invalid in a number of ways:
- It's not constant.
myEnum
doesn't have a|
operator.
You only got one of the errors.