apply<T extends DataGridRow> method

  1. @override
DataGridState<T>? apply<T extends DataGridRow>(
  1. EventContext<T> context
)
override

Apply this event's transformation to the state. Returns the new state, or null if no state change should occur. Can return a Future for async operations.

Implementation

@override
DataGridState<T>? apply<T extends DataGridRow>(EventContext<T> context) {
  if (!context.state.edit.isEditing) {
    return null;
  }

  final cellId = context.state.edit.editingCellId!;
  final parts = cellId.split('_');
  final rowId = double.parse(parts[0]);
  final columnId = int.parse(parts[1]);
  final newValue = context.state.edit.editingValue;

  // Apply the row update inline so that any events already queued (e.g.
  // CopyCellsEvent) see the new value immediately rather than reading stale
  // data from rowsById before a separately-dispatched UpdateCellEvent runs.
  final row = context.state.rowsById[rowId];
  final column = context.state.columns.firstWhere(
    (c) => c.id == columnId,
    orElse: () => throw Exception('Column $columnId not found'),
  );
  if (row != null && column.cellValueSetter != null) {
    column.cellValueSetter!(row, newValue);
  }
  final newRowsById = Map<double, T>.of(context.state.rowsById);
  context.dataIndexer.setData(newRowsById);

  return context.state.copyWith(
    edit: EditState.initial(),
    rowsById: newRowsById,
    selection: context.state.selection.copyWith(focusedCells: [cellId]),
  );
}