stringToEnum<T> function Null safety

T? stringToEnum<T>(
  1. String text,
  2. List<T> values
)

Converts a text into an enum value. Returns null if text is not valid.

Implementation

T? stringToEnum<T>(String text, List<T> values) {
  T? rc;
  if (text.isNotEmpty) {
    final suffix = '.' + text;
    for (var item in values) {
      if (item.toString().endsWith(suffix)) {
        rc = item;
        break;
      }
    }
  }
  return rc;
}