Enum from String

Mirrors aren’t always available, but fortunately you don’t need them. This is reasonably compact and should do what you want.

enum Fruit { apple, banana }

// Convert to string
String str = Fruit.banana.toString();

// Convert to enum
Fruit f = Fruit.values.firstWhere((e) => e.toString() == 'Fruit.' + str);

assert(f == Fruit.banana);  // it worked

Thanks to @frostymarvelous for correcting the answer

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)