In Flutter I Want to dynamic column in my Data Table because from database every time I get different number() of column. how I create Anyone help
1 Answer
Maybe you can use a StatefulWidget that wraps your DataTable, you can define
List<DataColumn> dataColumns; in your state.
And every time you get your source data from API/calculations/or somewhere. You can fill this dataColumns list from source data and invoke setState.
Let's say you fetch Column names form database and put it in a List<String> columnNames=['Name','Surname','Phone','Adress'];
setState(() {
dataColumns=columnNames.map((string)=>DataColumn(label: Text(string)).toList();
});
DataTable(
columns: dataColumns,
////....
)
4 Comments
Preeti Deswal
" dataColumns " how i create this variable , please one example
Preeti Deswal
because , i get error type '!rows.any((DataRow row) => row.cells.length != columns.length)': is not true.
Preeti Deswal
type 'List<String>' is not a subtype of type 'List<DataColumn>'
Suat Özkaya
Please check answer, I edited to make it more clear.