getShape static method

List<int> getShape(
  1. List list
)

Returns the shape of a list.

The shape is a list of the number of elements in each dimension.

Implementation

static List<int> getShape(List list) {
  List<int> shape = [];

  bool reachedEnd = false;
  List nestedList = list;
  while (!reachedEnd) {
    shape.add(nestedList.length);

    if (nestedList.isNotEmpty && nestedList[0] is List) {
      nestedList = nestedList[0];
    } else {
      reachedEnd = true;
    }
  }

  return shape;
}