getPart method

PartBuilder? getPart(
  1. MediaSubtype mediaSubtype, {
  2. bool recursive = true,
})

Retrieves the first builder with the specified mediaSubtype.

Unless rercursive is set to false, the whole tree is searched for the given mediaType.

Implementation

PartBuilder? getPart(MediaSubtype mediaSubtype, {bool recursive = true}) {
  final isPlainText = (mediaSubtype == MediaSubtype.textPlain);
  if (_children?.isEmpty ?? true) {
    if (contentType?.mediaType.sub == mediaSubtype ||
        (isPlainText && contentType == null)) {
      return this;
    }
    return null;
  }
  for (final child in _children!) {
    if (recursive) {
      final matchingPart = child.getPart(mediaSubtype);
      if (matchingPart != null) {
        return matchingPart;
      }
    } else if ((child.contentType?.mediaType.sub == mediaSubtype) ||
        (isPlainText && child.contentType == null)) {
      return child;
    }
  }
  return null;
}