iterableToShortString method

String iterableToShortString (
  1. Iterable iterable,
  2. [String leftDelimiter = '(',
  3. String rightDelimiter = ')']
)

Convert an Iterable to a string like IterableBase.toString.

Allows using other delimiters than '(' and ')'.

Handles circular references where converting one of the elements to a string ends up converting iterable to a string again.

Implementation

static String iterableToShortString(Iterable iterable,
    [String leftDelimiter = '(', String rightDelimiter = ')']) {
  if (_isToStringVisiting(iterable)) {
    if (leftDelimiter == "(" && rightDelimiter == ")") {
      // Avoid creating a new string in the "common" case.
      return "(...)";
    }
    return "$leftDelimiter...$rightDelimiter";
  }
  List<String> parts = <String>[];
  _toStringVisiting.add(iterable);
  try {
    _iterablePartsToStrings(iterable, parts);
  } finally {
    assert(identical(_toStringVisiting.last, iterable));
    _toStringVisiting.removeLast();
  }
  return (StringBuffer(leftDelimiter)
        ..writeAll(parts, ", ")
        ..write(rightDelimiter))
      .toString();
}

© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dart.dev/stable/2.13.0/dart-collection/IterableBase/iterableToShortString.html