joinToString method

String joinToString({
  1. String separator = ', ',
  2. String transform(
    1. E element
    )?,
  3. String prefix = '',
  4. String postfix = '',
  5. int? limit,
  6. String truncated = '...',
})

Creates a string from all the elements separated using separator and using the given prefix and postfix if supplied.

If the collection could be huge, you can specify a non-negative value of limit, in which case only the first limit elements will be appended, followed by the truncated string (which defaults to '...').

Implementation

String joinToString({
  String separator = ', ',
  String Function(E element)? transform,
  String prefix = '',
  String postfix = '',
  int? limit,
  String truncated = '...',
}) {
  var buffer = StringBuffer();
  var count = 0;
  for (var element in this) {
    if (limit != null && count >= limit) {
      buffer.write(truncated);
      return buffer.toString();
    }
    if (count > 0) {
      buffer.write(separator);
    }
    buffer.write(prefix);
    if (transform != null) {
      buffer.write(transform(element));
    } else {
      buffer.write(element.toString());
    }
    buffer.write(postfix);

    count++;
  }
  return buffer.toString();
}