enumFromString<T> function Null safety

T? enumFromString<T>(
  1. List<T>? enumValues,
  2. String? value
)

Implementation

T? enumFromString<T>(List<T>? enumValues, String? value)
{
  if (value == null || enumValues == null) return null;

  return value.contains(".") ?
      enumValues.singleWhere((x) => x.toString().toLowerCase() == value.toLowerCase(), orElse: () => null as T) :
      enumValues.singleWhere((x) => enumToString(x)?.toLowerCase() == value.toLowerCase(), orElse: () => null as T);
}