fromString<T> static method

T? fromString<T>(
  1. List<T> enumValues,
  2. String value,
  3. {bool camelCase = false}
)

Given a string, find and return its matching enum value

You need to pass in the values of the enum object. So TestEnum.values in the first argument. The matching value is the second argument.

Example final result = EnumToString.fromString(TestEnum.values, "valueOne") result == TestEnum.valueOne //true

Implementation

static T? fromString<T>(List<T> enumValues, String value,
    {bool camelCase = false}) {
  try {
    return enumValues.singleWhere((enumItem) =>
        EnumToString.convertToString(enumItem, camelCase: camelCase)
            .toLowerCase() ==
        value.toLowerCase());
  } on StateError catch (_) {
    return null;
  }
}