removeConsecutiveSpaces method

  1. @useResult
String? removeConsecutiveSpaces({
  1. bool trim = true,
})

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.

Implementation

@useResult
String? removeConsecutiveSpaces({bool trim = true}) {
  if (isEmpty) {
    return null;
  }
  final String replaced = replaceAll(_consecutiveSpacesRegex, ' ');

  return replaced.nullIfEmpty(trimFirst: trim);
}