getPart method

MimePart? getPart(
  1. String fetchId
)

Retrieves the part with the specified fetchId.

Returns null if the part has not been loaded (yet).

Implementation

MimePart? getPart(String fetchId) {
  if (fetchId.isEmpty) {
    throw StateError(
        'Invalid empty fetchId in MimeMessage.getPart(fetchId).');
  }
  final partsByFetchId = _individualParts;
  if (partsByFetchId != null) {
    final part = partsByFetchId[fetchId];
    if (part != null) {
      return part;
    }
  }
  final idParts = fetchId.split('.').map<int?>((part) => int.tryParse(part));
  MimePart parent = this;
  var warningGiven = false;
  for (final id in idParts) {
    if (id == null) {
      if (!warningGiven) {
        print(
            'Warning: unable to retrieve individual parts from fetchId [$fetchId] (in MimeMessage.getPart(fetchId)).');
        warningGiven = true;
      }
      continue;
    }
    final parts = parent.parts;
    if (parts == null || parts.length < id) {
      // this mime message is not fully loaded
      return null;
    }
    parent = parts[id - 1];
  }
  return parent;
}