getPart method
Retrieves the first builder with the specified mediaSubtype.
Unless recursive is set to false, the whole tree is searched
for the given mediaSubtype.
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;
}