shouldRecalculateColumnWidths method Null safety

  1. @protected
bool shouldRecalculateColumnWidths()

Called whenever you call notifyListeners or notifyDataSourceListeners in the DataGridSource class. If you want to recalculate all columns width (may be when underlying data gets changed), return true.

Returning true may impact performance as the column widths are recalculated again (whenever the notifyListeners is called).

If you are aware that column widths are going to be same whenever underlying data changes, return 'false' from this method.

Note: Column widths will be recalculated automatically whenever a new instance of DataGridSource is assigned to SfDataGrid.

class EmployeeDataSource extends DataGridSource {
  List<DataGridRow> get rows => _employees
      .map<DataGridRow>((dataRow) => DataGridRow(cells: [
            DataGridCell<int>(columnName: 'id', value:,
            DataGridCell<String>(columnName: 'name', value:,
                columnName: 'designation', value: dataRow.designation),
            DataGridCell<int>(columnName: 'salary', value: dataRow.salary),

  bool shouldRecalculateColumnWidths() {
    return true;

  DataGridRowAdapter? buildRow(DataGridRow row) {
    return DataGridRowAdapter(
        cells: row.getCells().map<Widget>((dataCell) {
          return Text(dataCell.value.toString());


bool shouldRecalculateColumnWidths() {
  return false;