I want to use the factory method in enum, but for some reason it is not available in my environment

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

Leave a Comment