xxxxxxxxxx
EnumName myEnum = <EnumName>.values.firstWhere((e) => describeEnum(e) == str);
xxxxxxxxxx
enum Topic { none, computing, general }
extension TopicString on String {
Topic get topic {
switch (this) {
case 'computing':
return Topic.computing;
case 'general':
return Topic.general;
case 'none':
return Topic.none;
}
}
}
extension TopicExtension on Topic {
String get string {
switch (this) {
case Topic.computing:
return 'computing';
case Topic.general:
return 'general';
case Topic.none:
return 'none';
}
}
}
xxxxxxxxxx
enum Day {
MONDAY("Monday"),
TUESDAY("Tuesday");
const Day(this.text);
final String text;
}
xxxxxxxxxx
Fruit f = Fruit.values.firstWhere((e) => e.toString() == 'Fruit.' + str);
xxxxxxxxxx
enum Color { red, green, blue }
Color getColorFromString(String colorString) {
switch (colorString) {
case "red":
return Color.red;
case "green":
return Color.green;
case "blue":
return Color.blue;
default:
throw Exception("Invalid color string: $colorString");
}
}
void main() {
String colorString = "green";
Color color = getColorFromString(colorString);
print(color);
}