getParent method Null safety

Mailbox? getParent(
  1. List<Mailbox> knownMailboxes,
  2. String separator,
  3. {bool create = true,
  4. bool createIntermediate = true}
)

Tries to determine the parent mailbox from the given knownMailboxes and separator.

Set create to false in case the parent should only be determined from the known mailboxes (defaults to true). Set createIntermediate to false and create to true to return the first known existing parent, when the direct parent is not known

Implementation

Mailbox? getParent(List<Mailbox> knownMailboxes, String separator,
    {bool create = true, bool createIntermediate = true}) {
  var lastSplitIndex = path.lastIndexOf(separator);
  if (lastSplitIndex == -1) {
    // this is a root mailbox, eg 'Inbox'
    return null;
  }
  var parentPath = path.substring(0, lastSplitIndex);
  var parent =
      knownMailboxes.firstWhereOrNull((box) => box.path == parentPath);
  if (parent == null && create) {
    lastSplitIndex = parentPath.lastIndexOf(separator);
    var parentName = (lastSplitIndex == -1)
        ? parentPath
        : parentPath.substring(lastSplitIndex + 1);
    parent = Mailbox.setup(parentName, parentPath, [MailboxFlag.noSelect]);
    if ((lastSplitIndex != -1) && (!createIntermediate)) {
      parent = parent.getParent(knownMailboxes, separator,
          create: true, createIntermediate: false);
    }
  }
  return parent;
}