apply<T extends DataGridRow> method

  1. @override
Future<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
Future<DataGridState<T>?> apply<T extends DataGridRow>(
  EventContext<T> context,
) async {
  final newRowsById = Map<double, T>.from(context.state.rowsById);
  final isNew = !newRowsById.containsKey(row.id);
  newRowsById[row.id] = row as T;

  final newDisplayOrder = List<double>.from(context.state.displayOrder);
  if (isNew) {
    if (position != null &&
        position! >= 0 &&
        position! <= newDisplayOrder.length) {
      newDisplayOrder.insert(position!, row.id);
    } else {
      newDisplayOrder.add(row.id);
    }
  }

  context.dataIndexer.setData(newRowsById);

  final filteredIds = context.state.filter.hasFilters
      ? await context.filterDelegate.applyFilters(
          rowsById: newRowsById,
          filters: context.state.filter.columnFilters.values.toList(),
          columns: context.state.columns,
        )
      : newDisplayOrder;

  final sortedIds = context.state.sort.hasSort
      ? context.dataIndexer.sortIds(
          newRowsById,
          filteredIds,
          context.state.sort.sortColumn!,
          context.state.columns,
        )
      : filteredIds;

  return context.state.copyWith(
    rowsById: newRowsById,
    displayOrder: sortedIds,
  );
}