addPart method Null safety

PartBuilder addPart(
  1. {ContentDispositionHeader? disposition,
  2. MimePart? mimePart,
  3. MediaSubtype? mediaSubtype,
  4. bool insert = false}
)

Adds a new part

Specifiy the optional disposition in case you want to specify the content-disposition Optionally specify the mimePart, if it is already known Optionally specify the mediaSubtype, e.g. MediaSubtype.multipartAlternative Optionally set insert to true to prepend and not append the part.

Implementation

PartBuilder addPart({
  ContentDispositionHeader? disposition,
  MimePart? mimePart,
  MediaSubtype? mediaSubtype,
  bool insert = false,
}) {
  final addAttachmentInfo = (mimePart != null &&
      mimePart.getHeaderContentDisposition()?.disposition ==
          ContentDisposition.attachment);
  mimePart ??= MimePart();
  final childBuilder = PartBuilder(mimePart);
  if (mediaSubtype != null) {
    childBuilder.setContentType(mediaSubtype.mediaType);
  } else if (mimePart.getHeaderContentType() != null) {
    childBuilder.contentType = mimePart.getHeaderContentType();
  }
  _children ??= <PartBuilder>[];
  if (insert) {
    _part.insertPart(mimePart);
    _children!.insert(0, childBuilder);
  } else {
    _part.addPart(mimePart);
    _children!.add(childBuilder);
  }
  disposition ??= mimePart.getHeaderContentDisposition();
  childBuilder.contentDisposition = disposition;
  if (mimePart.isTextMediaType()) {
    childBuilder.text = mimePart.decodeContentText();
  }
  if (addAttachmentInfo) {
    final info = AttachmentInfo(
        null,
        mimePart.mediaType,
        mimePart.decodeFileName(),
        disposition!.size,
        disposition.disposition,
        mimePart.decodeContentBinary(),
        childBuilder);
    attachments.add(info);
  }

  return childBuilder;
}