removeConsecutiveSpaces method
Returns a new string with consecutive whitespace collapsed into a single
space, or null if the result is empty.
When trim is true (default), the result is also trimmed.
Audited: 2026-06-12 11:26 EDT
Implementation
@useResult
String? removeConsecutiveSpaces({bool trim = true}) {
if (isEmpty) {
return null;
}
final String replaced = replaceAll(_consecutiveSpacesRegex, ' ');
return replaced.nullIfEmpty(trimFirst: trim);
}