ListGrid<E>.from constructor

ListGrid<E>.from(
  1. Grid<E> other, {
  2. E? empty,
})

Creates a new list grid from an existing grid.

The newer grid is a shallow copy of the existing grid, with the same size, position, and elements. The empty element is used as the default value, which defaults to other.empty if omitted.

Example

final grid = ListGrid.filled(2, 2, empty: 0);
grid.set(Pos(0, 0), 1);

final copy = ListGrid.from(grid);
print(copy.get(Pos(0, 0))); // 1

Implementation

factory ListGrid.from(Grid<E> other, {E? empty}) {
  if (other is _ListGrid<E>) {
    return _ListGrid(
      other._width,
      other._height,
      empty ?? other.empty,
      List<E>.from(other._elements),
    );
  }
  final list = List.of(other.cells.map((c) => c.$2));
  return _ListGrid(
    other.width,
    other.height,
    empty ?? other.empty,
    list,
  );
}