nullIfEmpty method

  1. @useResult
String? nullIfEmpty({
  1. bool trimFirst = true,
})

Returns null if the string is empty or contains only whitespace.

  • trimFirst: If true (default), the string is trimmed before the check. Audited: 2026-06-12 11:26 EDT

Implementation

@useResult
String? nullIfEmpty({bool trimFirst = true}) {
  if (isEmpty) {
    return null;
  }

  if (trimFirst) {
    final String trimmed = trim();

    return trimmed.isEmpty ? null : trimmed;
  }

  return this;
}