insertRowAt method

void insertRowAt(
  1. List<Map<String, dynamic>> rowsToInsert,
  2. int index
)

Insert rows at a specific index (surgical middle insert, no full rebuild) This is completely optional — existing insertRows() still works unchanged. Used for real-time WebSocket inserts at correct middle position.

Implementation

void insertRowAt(List<Map<String, dynamic>> rowsToInsert, int index) {
  if (rowsToInsert.isEmpty) return;

  final clampedIndex = index.clamp(0, _state.visibleRows.length);

  print('[TableController] ═══════════════════════════════════════');
  print('[TableController] ➕ insertRowAt: ${rowsToInsert.length} rows at index $clampedIndex');

  final updatedOriginalRows = List<Map<String, dynamic>>.from(_state.originalRows)
    ..insertAll(clampedIndex, rowsToInsert);
  final updatedRegularRows = List<Map<String, dynamic>>.from(_state.regularRows)
    ..insertAll(clampedIndex, rowsToInsert);
  final updatedVisibleRows = List<Map<String, dynamic>>.from(_state.visibleRows)
    ..insertAll(clampedIndex, rowsToInsert);

  _state = _state.copyWith(
    originalRows: updatedOriginalRows,
    regularRows: updatedRegularRows,
    visibleRows: updatedVisibleRows,
    totalRowCount: updatedVisibleRows.length,
  );

  notifyListeners();
  print('[TableController] ✅ Inserted ${rowsToInsert.length} rows at index $clampedIndex');
  print('[TableController] ═══════════════════════════════════════');
}