ListGrid<E>.withList constructor

ListGrid<E>.withList(
  1. List<E> elements, {
  2. required int width,
  3. required E empty,
})

Creates a new grid backed by the provided elements in row-major order.

The grid will have a width of width, and the number of rows is determined by the number of elements divided by the width, which must be an integer.

Changes to the provided list will be reflected in the grid, and changes to the grid will be reflected in the list accordingly, with elements[i] being the element at position Pos(i % width, i ~/ width).

The grid must be non-empty.

Warning

The provided list must not be modified in a way that violates the constraints of the grid, such as changing the length of the list, or changing the elements in a way that violates the width of the grid.

If the list will support appending rows or columns at runtime, access must carefully avoid modifying the length during iteration, including by ListGrid.columns, ListGrid.rows, or any other iterative operation.

Performance

This constructor exists in order to avoid copying the elements of the list into a new list, which can be expensive for large lists, or for using a different sub-type of List such as Uint8List or Float32List.

Implementation

factory ListGrid.withList(
  List<E> elements, {
  required int width,
  required E empty,
}) {
  checkPositive(width, 'width');
  final (_, size) = checkRectangular1D(elements, width: width);
  final height = size.y;
  return _ListGrid(width, height, empty, elements);
}