flutterdartdatatabledynamic-columns

In flutter how to create dynamic column and row both (according to data column ) in datatable


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


Solution

  • 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,
              ////....
        
        )