enumFromString<T extends Object> function

T enumFromString<T extends Object>(
  1. Iterable<T> enumValues,
  2. String value
)

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;
  });
}