onBeforeStateUpdate method

  1. @override
DataGridState<T>? onBeforeStateUpdate(
  1. DataGridState<T> newState,
  2. DataGridState<T> oldState,
  3. DataGridEvent? event
)
override

Called after event is processed but before state is updated.

Return:

  • The same state to continue with the update
  • A modified state to change what gets applied
  • null to cancel the state update

Implementation

@override
DataGridState<T>? onBeforeStateUpdate(
  DataGridState<T> newState,
  DataGridState<T> oldState,
  DataGridEvent? event,
) {
  if (event is CommitCellEditEvent &&
      onCellCommit != null &&
      oldState.edit.isEditing) {
    final cellId = oldState.edit.editingCellId!;
    final parts = cellId.split('_');
    final rowId = double.parse(parts[0]);
    final columnId = int.parse(parts[1]);
    final oldValue = oldState.rowsById[rowId];
    final newValue = oldState.edit.editingValue;

    onCellCommit!(rowId, columnId, oldValue, newValue).then((allowed) {
      if (!allowed) {
        return oldState;
      }
    });
  }

  return newState;
}