flattenDepth<T> function

List<T>? flattenDepth<T>(
  1. List<T> list, [
  2. int depth = 1
])

Recursively flatten array up to depth times.

It returns newObject of flattened array and does not affects the list object called-upon

var list = [2, [1, 3], [4, [1, [2]] ] ];
var newList = flattenDepth(list, 1); // newList = [2, 1, 3, 4, [1, [2] ] ];

Implementation

List<T>? flattenDepth<T>(List<T> list, [int depth = 1]) {
  return list.flattenDepth(depth) as List<T>?;
}