codecForContentType method Null safety

Codec<dynamic, List<int>>? codecForContentType(
  1. ContentType? contentType
)

Returns a Codec for contentType.

See add.

Implementation

Codec<dynamic, List<int>>? codecForContentType(ContentType? contentType) {
  if (contentType == null) {
    return null;
  }

  Codec? contentCodec;
  Codec<String, List<int>>? charsetCodec;

  var subtypes = _fullySpecificedCodecs[contentType.primaryType];
  if (subtypes != null) {
    contentCodec = subtypes[contentType.subType];
  }

  contentCodec ??= _primaryTypeCodecs[contentType.primaryType];

  if ((contentType.charset?.length ?? 0) > 0) {
    charsetCodec = _codecForCharset(contentType.charset);
  } else if (contentType.primaryType == "text" && contentCodec == null) {
    charsetCodec = latin1;
  } else {
    charsetCodec = _defaultCharsetCodecForType(contentType);
  }

  if (contentCodec != null) {
    if (charsetCodec != null) {
      return contentCodec.fuse(charsetCodec);
    }
    if (contentCodec is! Codec<dynamic, List<int>>) {
      throw StateError("Invalid codec selected. Does not emit 'List<int>'.");
    }
    return contentCodec;
  }

  if (charsetCodec != null) {
    return charsetCodec;
  }

  return null;
}