tryParse method
Tries to parse an enum
constant from its name.
Ignores surrounding whitespace.
Returns null
if unrecognized.
Example:
enum Color { red, green, blue }
Color.values.tryParse('blue'); // Color.blue
Implementation
T? tryParse(String? name, {bool caseSensitive = false}) {
if (name == null) {
return null;
}
name = name.trim();
if (!caseSensitive) {
name = name.toLowerCase();
}
for (var element in this) {
var enumName = element.name;
if (!caseSensitive) {
enumName = enumName.toLowerCase();
}
if (enumName == name) {
return element;
}
}
return null;
}