flattenDepth function

List flattenDepth(
  1. List array,
  2. int depth
)

Recursively flatten array up to depth times.

Implementation

List flattenDepth(List array, int depth) {
  if (depth == 1) {
    return flatten(array);
  }
  return array.expand((element) {
    if (element is List) {
      return flattenDepth(element, depth - 1);
    }
    return [element];
  }).toList();
}