valueOfOrThrow method

T valueOfOrThrow(
  1. String? value
)

Like valueOf, but throws instead of returning null when value is null or matches no enum value in this iterable.

Matching is the same as valueOf: case-insensitive, with surrounding whitespace trimmed.

Throws an ArgumentError naming value and the names it was compared against.

{@tool snippet}

enum Status { pending, active, done }

void main() {
  print(Status.values.valueOfOrThrow('ACTIVE')); // Status.active
  Status.values.valueOfOrThrow('archived'); // throws ArgumentError
}

{@end-tool}

Implementation

T valueOfOrThrow(String? value) {
  final match = valueOf(value);
  if (match != null) return match;
  final names = map((type) => type.name).join(', ');
  throw ArgumentError.value(
    value,
    'value',
    'No enum value matches (case-insensitive) among [$names]',
  );
}