ListGrid<T>.fromRows constructor

ListGrid<T>.fromRows(
  1. Iterable<Iterable<T>> rows
)

Creates a new grid from the given rows.

Each row must have the same length.

Implementation

factory ListGrid.fromRows(Iterable<Iterable<T>> rows) {
  if (rows.isEmpty) {
    return ListGrid.empty();
  }

  final rows_ = List.of(rows);
  final width = rows.first.length;
  for (final row in rows_) {
    if (row.length != width) {
      throw ArgumentError.value(
        rows,
        'rows',
        'All rows must have the same length.',
      );
    }
  }

  final cells = rows_.expand((row) => row).toList();
  return ListGrid._(cells, width);
}