dumpIterable function

String dumpIterable(
  1. Iterable it, {
  2. int maxItems = 10,
})

Renders it as a string, truncating to the first maxItems elements and appending the total count when the iterable is longer.

Avoids dumping huge collections in logs while still showing a representative head and the real size.

Example:

dumpIterable([1, 2, 3, 4], maxItems: 2); // '[1, 2]... (4 total)'

Implementation

String dumpIterable(Iterable<dynamic> it, {int maxItems = 10}) {
  final List<dynamic> list = it.toList();
  if (list.length <= maxItems) return list.toString();
  // ignore: saropa_lints/avoid_large_list_copy -- needs an independent copy to render the truncated head as a list literal in toString
  return '${list.take(maxItems).toList()}... (${list.length} total)';
}