apply<TRow extends DataGridRow> method

  1. @override
Future<DataGridState<TRow>?> apply<TRow extends DataGridRow>(
  1. EventContext<TRow> 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<TRow>?> apply<TRow extends DataGridRow>(
  EventContext<TRow> context,
) async {
  final rowsMap = {for (var row in rows) (row as TRow).id: row as TRow};
  final newRowsById = append
      ? {...context.state.rowsById, ...rowsMap}
      : rowsMap;

  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,
        )
      : newRowsById.keys.toList();

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

  final totalItems = sortedIds.length;

  List<double> finalDisplayOrder;
  if (context.state.pagination.enabled &&
      !context.state.pagination.serverSide) {
    final startIndex = context.state.pagination.startIndex(totalItems);
    final endIndex = context.state.pagination.endIndex(totalItems);
    finalDisplayOrder = sortedIds.sublist(
      math.min(startIndex, sortedIds.length),
      math.min(endIndex, sortedIds.length),
    );
  } else {
    finalDisplayOrder = sortedIds;
  }

  return context.state.copyWith(
    rowsById: newRowsById,
    displayOrder: finalDisplayOrder,
    totalItems: totalItems,
    isLoading: false,
  );
}