$enumDecode<K extends Enum, V> function

K $enumDecode<K extends Enum, V>(
  1. Map<K, V> enumValues,
  2. Object? source, {
  3. K? unknownValue,
})

Returns the key associated with value source from enumValues, if one exists.

If unknownValue is not null and source is not a value in enumValues, unknownValue is returned. Otherwise, an ArgumentError is thrown.

If source is null, an ArgumentError is thrown.

Exposed only for code generated by package:json_serializable. Not meant to be used directly by user code.

Implementation

K $enumDecode<K extends Enum, V>(
  Map<K, V> enumValues,
  Object? source, {
  K? unknownValue,
}) {
  if (source == null) {
    throw ArgumentError(
      'A value must be provided. Supported values: '
      '${enumValues.values.join(', ')}',
    );
  }

  for (var entry in enumValues.entries) {
    if (entry.value == source) {
      return entry.key;
    }
  }

  if (unknownValue == null) {
    throw ArgumentError(
      '`$source` is not one of the supported values: '
      '${enumValues.values.join(', ')}',
    );
  }

  return unknownValue;
}