$enumDecodeNullable<K extends Enum, V> function Null safety

K? $enumDecodeNullable<K extends Enum, V>(
  1. Map<K, V> enumValues,
  2. Object? source,
  3. {Object? 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, {
  Object? 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(', ')}',
    );
  }

  return unknownValue as K;
}