change method

MediaType change({
  1. String? type,
  2. String? subtype,
  3. String? mimeType,
  4. Map<String, String>? parameters,
  5. bool clearParameters = false,
})

Returns a copy of this MediaType with some fields altered.

type and subtype alter the corresponding fields. mimeType is parsed and alters both the type and subtype fields; it cannot be passed along with type or subtype.

parameters overwrites and adds to the corresponding field. If clearParameters is passed, it replaces the corresponding field entirely instead.

Implementation

MediaType change(
    {String? type,
    String? subtype,
    String? mimeType,
    Map<String, String>? parameters,
    bool clearParameters = false}) {
  if (mimeType != null) {
    if (type != null) {
      throw ArgumentError('You may not pass both [type] and [mimeType].');
    } else if (subtype != null) {
      throw ArgumentError('You may not pass both [subtype] and '
          '[mimeType].');
    }

    final segments = mimeType.split('/');
    if (segments.length != 2) {
      throw FormatException('Invalid mime type "$mimeType".');
    }

    type = segments[0];
    subtype = segments[1];
  }

  type ??= this.type;
  subtype ??= this.subtype;
  parameters ??= {};

  if (!clearParameters) {
    final newParameters = parameters;
    parameters = Map.from(this.parameters);
    parameters.addAll(newParameters);
  }

  return MediaType(type, subtype, parameters);
}