sortMailboxes method Null safety

List<Mailbox> sortMailboxes(
  1. List<MailboxFlag> order,
  2. List<Mailbox> mailboxes,
  3. {bool keepRemaining = true,
  4. bool sortRemainingAlphabetically = true}
)

Retrieves the mailbox with the specified order from the provided mailboxes. The underlying mailboxes are not changed.

Set keepRemaining to false (defaults to true) to only return the mailboxes specified by the order MailboxFlags. Set sortRemainingAlphabetically to false (defaults to true) to sort the remaining boxes by name, is only relevant when keeyRemaining is true.

Implementation

List<Mailbox> sortMailboxes(List<MailboxFlag> order, List<Mailbox> mailboxes,
    {bool keepRemaining = true, bool sortRemainingAlphabetically = true}) {
  final inputMailboxes = <Mailbox>[...mailboxes];
  final outputMailboxes = <Mailbox>[];
  for (final flag in order) {
    final box = getMailbox(flag, inputMailboxes);
    if (box != null) {
      outputMailboxes.add(box);
      inputMailboxes.remove(box);
    }
  }
  if (keepRemaining) {
    if (sortRemainingAlphabetically) {
      inputMailboxes.sort((b1, b2) => b1.path.compareTo(b2.path));
    }
    outputMailboxes.addAll(inputMailboxes);
  }
  return outputMailboxes;
}