We have defined the following enum.
enum City {
us,
korea,
canada,
other;
factory City.fromName(String name) => switch (name) {
'us' => City.us,
'korea' => City.korea,
'canada' => City.canada,
_ => City.other,
};
}
And I would like to use it this way, but it is not available in my environment.
City.fromName(city);
If you write it like this, you can use it.
City.korea.fromName(city);
I heard that others can use City.fromName(city);
but not me.
How can I use the factory method in enum?
Flutter: 3.16.5
Dart: 3.2.3
Can you provide a link where it shows that others can use
City.fromName(city)
?This way. reddit.com/r/flutterhelp/comments/18p2p7e/comment/keld5i9/…
I tried your code in DartPad, it works just fine dartpad.dev/?id=cf402bbb1e0a3b1e5a305a57648208e7
Yes, I know. I know. That’s why I’m asking the question here, because I’m having trouble finding the cause.
But you didn’t mention what problem you get there. It’s not clear what you meant by “not available in my environment”
Show 2 more comments