contentType property Null safety

ContentType? contentType

The content type of the body of this response.

Defaults to defaultContentType. This response's body will be encoded according to this value. The Content-Type header of the HTTP response will always be set according to this value.

If this value is set directly, then this instance's Content-Type will be that value. If this value is not set, then the headers property is checked for the key 'content-type'. If the key is not present in headers, this property's value is defaultContentType.

If the key is present and the value is a String, this value is the result of passing the value to ContentType.parse. If the key is present and the value is a ContentType, this property is equal to that value.

Implementation

ContentType? get contentType {
  if (_contentType != null) {
    return _contentType;
  }

  final inHeaders = _headers[HttpHeaders.contentTypeHeader];
  if (inHeaders == null) {
    return defaultContentType;
  }

  if (inHeaders is ContentType) {
    return inHeaders;
  }

  if (inHeaders is String) {
    return ContentType.parse(inHeaders);
  }

  throw StateError(
    "Invalid content-type response header. Is not 'String' or 'ContentType'.",
  );
}
void contentType=(ContentType? t)

Implementation

set contentType(ContentType? t) {
  _contentType = t;
}