separatedBy method

List<T> separatedBy(
  1. T separator
)

Creates a new list from the iterable by interspersing a given separator between each element. If the iterable contains less than two elements, the original iterable is returned as a list.

Example:

final letters = ['a', 'b', 'c'];
final spacedLetters = letters.separatedBy('-');
print(spacedLetters); // ['a', '-', 'b', '-', 'c']

Implementation

List<T> separatedBy(T separator) {
  if (length < 2) return toList();
  return [first, separator, ...skip(1).separatedBy(separator)];
}