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.

Implementation

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

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

    return trimmed.isEmpty ? null : trimmed;
  }

  return this;
}