$enumDecodeNullable<K extends Enum, V> function

K? $enumDecodeNullable<K extends Enum, V>(
  1. Map<K, V> enumValues,
  2. Object? source, {
  3. Enum? 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, null is returned.

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

Implementation

K? $enumDecodeNullable<K extends Enum, V>(
  Map<K, V> enumValues,
  Object? source, {
  Enum? unknownValue,
}) {
  if (source == null) {
    return null;
  }

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

  if (unknownValue == JsonKey.nullForUndefinedEnumValue) {
    return null;
  }

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

  if (unknownValue is! K) {
    throw ArgumentError.value(
      unknownValue,
      'unknownValue',
      'Must by of type `$K` or `JsonKey.nullForUndefinedEnumValue`.',
    );
  }

  return unknownValue;
}