|
1 | | -# how-to-set-the-key-property-for-each-row-in-a-flutter-data-table |
2 | | -How to set the key property for each row in a Flutter DataTable (SfDataGrid) |
| 1 | +# How to set the key property for each row in a Flutter DataTable (SfDataGrid)? |
| 2 | + |
| 3 | +In this article, we'll discuss how to set the key property for each row in a [Flutter DataGrid](https://www.syncfusion.com/flutter-widgets/flutter-datagrid) within the [SfDataGrid](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/SfDataGrid-class.html) widget for automation purposes. |
| 4 | + |
| 5 | +## STEP 1: |
| 6 | +Initialize the [SfDataGrid](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/SfDataGrid-class.html) widget with all the required properties. |
| 7 | + |
| 8 | + ```dart |
| 9 | +@override |
| 10 | + void initState() { |
| 11 | + super.initState(); |
| 12 | + employees = getEmployeeData(); |
| 13 | + employeeDataSource = EmployeeDataSource(employeeData: employees); |
| 14 | + } |
| 15 | +
|
| 16 | + @override |
| 17 | + Widget build(BuildContext context) { |
| 18 | + return Scaffold( |
| 19 | + appBar: AppBar( |
| 20 | + title: const Text('Syncfusion Flutter DataGrid'), |
| 21 | + ), |
| 22 | + body: SfDataGrid( |
| 23 | + source: employeeDataSource, |
| 24 | + columnWidthMode: ColumnWidthMode.fill, |
| 25 | + columns: <GridColumn>[ |
| 26 | + GridColumn( |
| 27 | + columnName: 'id', |
| 28 | + label: Container( |
| 29 | + padding: const EdgeInsets.all(16.0), |
| 30 | + alignment: Alignment.center, |
| 31 | + child: const Text( |
| 32 | + 'ID', |
| 33 | + ))), |
| 34 | + GridColumn( |
| 35 | + columnName: 'name', |
| 36 | + label: Container( |
| 37 | + padding: const EdgeInsets.all(8.0), |
| 38 | + alignment: Alignment.center, |
| 39 | + child: const Text('Name'))), |
| 40 | + GridColumn( |
| 41 | + columnName: 'designation', |
| 42 | + label: Container( |
| 43 | + padding: const EdgeInsets.all(8.0), |
| 44 | + alignment: Alignment.center, |
| 45 | + child: const Text( |
| 46 | + 'Designation', |
| 47 | + overflow: TextOverflow.ellipsis, |
| 48 | + ))), |
| 49 | + GridColumn( |
| 50 | + columnName: 'salary', |
| 51 | + label: Container( |
| 52 | + padding: const EdgeInsets.all(8.0), |
| 53 | + alignment: Alignment.center, |
| 54 | + child: const Text('Salary'))), |
| 55 | + ], |
| 56 | + ), |
| 57 | + ); |
| 58 | + } |
| 59 | +
|
| 60 | + ``` |
| 61 | + |
| 62 | +## STEP 2: |
| 63 | +The buildRow method in the [DataGridSource](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/DataGridSource-class.html) class creates a [DataGridRowAdapter](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/DataGridRowAdapter-class.html) for every row in the DataGrid. The DataGridRowAdapter is used to customize how each row looks and behaves. To assign a unique identifier to each DataGridRowAdapter, a [Key](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/DataGridRowAdapter/key.html) is used. This key allows easy referencing and interaction with specific rows in the [SfDataGrid](https://pub.dev/documentation/syncfusion_flutter_datagrid/latest/datagrid/SfDataGrid-class.html), enabling automation and other operations that require identifying individual rows. |
| 64 | + |
| 65 | +```dart |
| 66 | +class EmployeeDataSource extends DataGridSource { |
| 67 | + EmployeeDataSource({required List<Employee> employeeData}) { |
| 68 | + _employeeData = employeeData |
| 69 | + .map<DataGridRow>((e) => DataGridRow(cells: [ |
| 70 | + DataGridCell<int>(columnName: 'id', value: e.id), |
| 71 | + DataGridCell<String>(columnName: 'name', value: e.name), |
| 72 | + DataGridCell<String>( |
| 73 | + columnName: 'designation', value: e.designation), |
| 74 | + DataGridCell<int>(columnName: 'salary', value: e.salary), |
| 75 | + ])) |
| 76 | + .toList(); |
| 77 | + } |
| 78 | +
|
| 79 | + List<DataGridRow> _employeeData = []; |
| 80 | +
|
| 81 | + @override |
| 82 | + List<DataGridRow> get rows => _employeeData; |
| 83 | +
|
| 84 | + @override |
| 85 | + DataGridRowAdapter buildRow(DataGridRow row) { |
| 86 | + final index = _employeeData.indexOf(row); |
| 87 | + final ValueKey<String> valueKey = ValueKey<String>('employee_$index'); |
| 88 | + return DataGridRowAdapter( |
| 89 | + key: valueKey, |
| 90 | + cells: row.getCells().map<Widget>((e) { |
| 91 | + return Container( |
| 92 | + alignment: Alignment.center, |
| 93 | + padding: const EdgeInsets.all(8.0), |
| 94 | + child: Text(e.value.toString()), |
| 95 | + ); |
| 96 | + }).toList()); |
| 97 | + } |
| 98 | +} |
| 99 | +
|
| 100 | + ``` |
0 commit comments