I am having this enum:
enum ContentError {
e410('Exception: 410'),
e411('Excetion: 411'),
e412('Exception: 412'),
e413('Exception: 413');
const ContentError(this.name);
final String name;
}
Now I get some String and I want to do a switch case on the names of the Enum.
switch (myString) {
case ContentError.e410.name:
// do something
...
}
but this does not work as it says The property 'name' can't be accessed on the type 'ContentError' in a constant expression.
How can I do a switch case on the Enum attriute name
?
2
Answers
Try this!
You can change the switch case and define the action each case using map and returning the method to execute correct conditions, the step explained in the code below:
And this is the results:
Hopefully it can solve your problem, Thanks 😉