joinToString method

String joinToString({
  1. String separator = ", ",
  2. String prefix = "",
  3. String postfix = "",
  4. int limit = -1,
  5. String truncated = "...",
  6. String transform(
    1. T
    )?,
})

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 prefix = "",
    String postfix = "",
    int limit = -1,
    String truncated = "...",
    String Function(T)? transform}) {
  final buffer = StringBuffer();
  buffer.write(prefix);
  var count = 0;
  for (final element in iter) {
    if (++count > 1) buffer.write(separator);
    if (limit >= 0 && count > limit) {
      break;
    } else {
      if (transform == null) {
        buffer.write(element);
      } else {
        buffer.write(transform(element));
      }
    }
  }
  if (limit >= 0 && count > limit) {
    buffer.write(truncated);
  }
  buffer.write(postfix);
  return buffer.toString();
}