nullIfEmpty method
Returns null
if the string is empty or contains only whitespace.
trimFirst
: If true (default), the string is trimmed before the check. Otherwise, returns the (potentially trimmed) string.
Implementation
String? nullIfEmpty({bool trimFirst = true}) {
// Handle the empty string case directly.
if (isEmpty) {
return null;
}
// If trimFirst is enabled, trim the string before checking for emptiness.
if (trimFirst) {
final String trimmed = trim();
return trimmed.isEmpty ? null : trimmed;
}
// Otherwise, return the original string.
return this;
}