initializeRows static method

List<PlutoRow> initializeRows(
  1. List<PlutoColumn> refColumns,
  2. List<PlutoRow> refRows, {
  3. bool forceApplySortIdx = true,
  4. bool increase = true,
  5. int start = 0,
})

It handles the necessary settings when rows are first set or added to the PlutoGrid.

forceApplySortIdx determines whether to force PlutoRow.sortIdx to be set.

increase and start are valid only when forceApplySortIdx is true.

increase determines whether to increment or decrement when initializing sortIdx. For example, if a row is added before an existing row, the sortIdx value should be set to a negative number than the row being added.

start sets the starting value when initializing sortIdx. For example, if sortIdx is set from 0 to 9 in the previous 10 rows, start is set to 10, which sets the sortIdx of the row added at the end.

It is created when PlutoGrid is first created, and the state required for the grid is set for List<PlutoRow> rows. PlutoGridStateManager.initializeRows, which operates at this time, works synchronously, and if there are many rows, the UI may freeze when starting the grid.

To prevent UI from freezing when passing many rows to PlutoGrid, you can set rows asynchronously as follows. After passing an empty list when creating PlutoGrid, add rows initialized with initializeRowsAsync as shown below.

PlutoGridStateManager.initializeRowsAsync(
  columns,
  fetchedRows,
).then((value) {
  stateManager.refRows.addAll(FilteredList(initialList: value));
  stateManager.notifyListeners();
});

Implementation

static List<PlutoRow> initializeRows(
  List<PlutoColumn> refColumns,
  List<PlutoRow> refRows, {
  bool forceApplySortIdx = true,
  bool increase = true,
  int start = 0,
}) {
  if (refColumns.isEmpty || refRows.isEmpty) {
    return refRows;
  }

  _ApplyList applyList = _ApplyList([
    _ApplyCellForSetColumnRow(refColumns),
    _ApplyRowForSortIdx(
      forceApply: forceApplySortIdx,
      increase: increase,
      start: start,
      firstRow: refRows.first,
    ),
    _ApplyRowGroup(refColumns),
  ]);

  if (!applyList.apply) {
    return refRows;
  }

  var rowLength = refRows.length;

  for (var rowIdx = 0; rowIdx < rowLength; rowIdx += 1) {
    applyList.execute(refRows[rowIdx]);
  }

  return refRows;
}