enumFromString<T> function

T? enumFromString<T>(
  1. List<T> values,
  2. String? comp
)

Looks up an enum value by its string representation.

Given values (typically SomeEnum.values) and comp (for example the string returned from a platform channel), returns the matching enum entry or null when no entry matches or comp is null.

Example:

final state = enumFromString(BluetoothState.values, 'on');
// state == BluetoothState.on

Implementation

T? enumFromString<T>(List<T> values, String? comp) {
  if (comp == null) return null;
  for (final value in values) {
    if (value.toString().split('.').last == comp) {
      return value;
    }
  }
  return null;
}