removeTrimmedEmpty method

  1. @useResult
List<String>? removeTrimmedEmpty({
  1. bool trim = true,
})

Drops entries that are empty after trimming, returning a new list.

With trim true (default) the survivors are also trimmed; with trim false only literally empty ('') entries are dropped — a whitespace-only ' ' is kept untouched. Returns null (never []) when nothing remains, so a caller distinguishes "no items" from a real result. Does not mutate the receiver.

Example:

[' a ', 'b'].removeTrimmedEmpty();              // ['a', 'b']
[' a ', '   '].removeTrimmedEmpty(trim: false); // [' a ']
['', '  '].removeTrimmedEmpty();                // null

Audited: 2026-06-12 11:26 EDT

Implementation

@useResult
List<String>? removeTrimmedEmpty({bool trim = true}) =>
    // The per-element nullable trimmer enforces the trim-vs-keep contract,
    // the null filter strips dropped entries, and the list helper collapses
    // an empty result to null rather than an empty list.
    map((String e) => e.nullIfEmpty(trimFirst: trim)).nonNulls.toList().nullIfEmpty();