enumFromString<T extends Object> function
Given a string, find and return its matching enum
value
This will try to match both strings in camelCase and with underscores.
This is also case sensitive.
enum TestEnum { valueOne, valueTwo }
final eNum = enumFromString(TestEnum.values, 'valueOne');
eNum == TestEnum.valueOne; // true
Implementation
T enumFromString<T extends Object>(Iterable<T> enumValues, String value) {
return enumValues.singleWhere((enumItem) {
final eNum = enumToString(enumItem, withUnderscores: false);
return eNum == value || eNum.withUnderscores() == value;
});
}