Preeti Deswal
Preeti Deswal

Reputation: 15

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

Upvotes: 0

Views: 1649

Answers (1)

Suat Özkaya
Suat Özkaya

Reputation: 755

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

Upvotes: 1

Related Questions