joinWithFinal method

  1. @useResult
String? joinWithFinal({
  1. String separator = ', ',
  2. String finalSeparator = 'and',
})

Joins these strings into a sentence with a distinct final connector, e.g. ['a', 'b', 'c'].joinWithFinal() -> 'a, b and c'.

Unlike joinDisplayList this adds no Oxford comma before the final connector (British style), and it does no trimming or de-duplication — a blank entry is kept, so ['a', '', 'c'] yields 'a, and c'. Returns null for an empty list and the sole element for a single-item list, so a caller can tell "no items" apart from a real joined string.

  • separator: placed between the leading items (default ', ').
  • finalSeparator: the connector word before the last item (default 'and'); it is surrounded by single spaces.

Example:

['Alice', 'Bob'].joinWithFinal();          // 'Alice and Bob'
['Alice', 'Bob', 'Carol'].joinWithFinal(); // 'Alice, Bob and Carol'
<String>[].joinWithFinal();                // null

Audited: 2026-06-12 11:26 EDT

Implementation

@useResult
String? joinWithFinal({String separator = ', ', String finalSeparator = 'and'}) {
  if (isEmpty) {
    return null;
  }

  if (length == 1) {
    return firstOrNull;
  }

  // Drop the final element via a sublist so [finalSeparator] (not
  // [separator]) precedes it; `last` is safe because length >= 2 here.
  return '${sublist(0, length - 1).join(separator)} '
      '$finalSeparator $last';
}