updateRows method

void updateRows(
  1. List<Map<String, dynamic>> rowsToUpdate
)

Update rows (handles 1 row or 1,000 rows, row_id OR temp_row_id) Matches web pattern: tabulator-formula.service.ts:1195-1263

Implementation

void updateRows(List<Map<String, dynamic>> rowsToUpdate) {
  if (rowsToUpdate.isEmpty) {
    print('[TableController] ⚠️ updateRows: Empty list');
    return;
  }

  print('[TableController] ═══════════════════════════════════════');
  print('[TableController] 🔄 updateRows: ${rowsToUpdate.length} rows');
  print('[TableController] 📦 First row: ${rowsToUpdate[0]}');

  // Build lookup map indexed by both row_id and temp_row_id (O(1) lookup)
  final Map<String, Map<String, dynamic>> updateMapById = {};

  for (final row in rowsToUpdate) {
    final rawRowId = row['row_id'];
    bool indexed = false;
    if (rawRowId != null) {
      updateMapById[rawRowId.toString()] = row;
      indexed = true;
    }
    // Also index by temp_row_id so visibleRows with row_id=null can still be
    // found after replace_data assigns a permanent row_id to the update row.
    final tempId = row['temp_row_id']?.toString();
    if (tempId != null && tempId.isNotEmpty) {
      updateMapById[tempId] = row;
      indexed = true;
    }
    if (!indexed) {
      print('[TableController] ⚠️ Row has neither row_id nor temp_row_id!');
    }
  }

  print('[TableController] 📊 updateMapById size: ${updateMapById.length}');

  int updatedCount = 0;

  print('[TableController] 🔍 Updating originalRows (${_state.originalRows.length} rows)...');

  // Update originalRows
  final updatedOriginalRows = _state.originalRows.map((row) {
    // Try row_id first
    final rowId = row['row_id']?.toString();
    if (rowId != null && updateMapById.containsKey(rowId)) {
      updatedCount++;
      return {...row, ...updateMapById[rowId]!};
    }

    // Fallback to temp_row_id (handles newly inserted rows where row_id is still null)
    final tempId = row['temp_row_id']?.toString();
    if (tempId != null && tempId.isNotEmpty && updateMapById.containsKey(tempId)) {
      updatedCount++;
      return {...row, ...updateMapById[tempId]!};
    }

    return row;
  }).toList();

  print('[TableController] 📊 Updated $updatedCount rows in originalRows');

  if (updatedCount == 0) {
    print('[TableController] ❌ NO ROWS MATCHED! Debugging info:');
    print('[TableController]   - Looking for row_id: ${rowsToUpdate[0]['row_id']}');
    print('[TableController]   - Looking for temp_row_id: ${rowsToUpdate[0]['temp_row_id']}');
    if (_state.originalRows.isNotEmpty) {
      print('[TableController]   - First row in table row_id: ${_state.originalRows[0]['row_id']}');
      print('[TableController]   - First row in table temp_row_id: ${_state.originalRows[0]['temp_row_id']}');
      print('[TableController]   - First row in table _id: ${_state.originalRows[0]['_id']}');
    }
  }

  // Update regularRows (same logic)
  final updatedRegularRows = _state.regularRows.map((row) {
    final rowId = row['row_id']?.toString();
    if (rowId != null && updateMapById.containsKey(rowId)) {
      return {...row, ...updateMapById[rowId]!};
    }

    final tempId = row['temp_row_id']?.toString();
    if (tempId != null && tempId.isNotEmpty && updateMapById.containsKey(tempId)) {
      return {...row, ...updateMapById[tempId]!};
    }

    return row;
  }).toList();

  // Update visibleRows (same logic)
  final updatedVisibleRows = _state.visibleRows.map((row) {
    final rowId = row['row_id']?.toString();
    if (rowId != null && updateMapById.containsKey(rowId)) {
      return {...row, ...updateMapById[rowId]!};
    }

    final tempId = row['temp_row_id']?.toString();
    if (tempId != null && tempId.isNotEmpty && updateMapById.containsKey(tempId)) {
      return {...row, ...updateMapById[tempId]!};
    }

    return row;
  }).toList();

  // Single state update + Single notifyListeners
  print('[TableController] 🔄 Updating state...');
  _state = _state.copyWith(
    originalRows: updatedOriginalRows,
    regularRows: updatedRegularRows,
    visibleRows: updatedVisibleRows,
  );

  print('[TableController] 🔔 Calling notifyListeners...');
  notifyListeners();

  print('[TableController] ✅ Updated $updatedCount rows total');
  print('[TableController] ═══════════════════════════════════════');
}