Flutter - Creating tables dynamically Flutter - Creating tables dynamically dart dart

Flutter - Creating tables dynamically


This function creates a table dynamically:

  Widget createTable() {    List<TableRow> rows = [];    for (int i = 0; i < 100; ++i) {      rows.add(TableRow(children: [        Text("number " + i.toString()),        Text("squared " + (i * i).toString()),      ]));    }    return Table(children: rows);  }


First fetch List of Records

List<PriceDetailsView> priceDetailsList = MockDataSource.getPriceDetailsDataList();

Now create an empty list of table rows :

List<TableRow> priceTableRows = [];

Add details from the fetched list to this row list:

for(PriceDetailsView priceDetalis in priceDetailsList){      priceTableRows.add(TableRow(children: [Text(priceDetalis.priceType), Text(priceDetalis.priceValue)]));    }

Now create your table with this list of row :

Table priceTable = Table(children: priceTableRows);


It's pretty easy, actually! All you have to do is, make a list of TableRows, and put that in the children parameter of your table. For example

List<TableRow> tableRows = [];// dynamically make TableRows and add them to the list

And then you can just do this:

Table(   children: tableRows,   // other stuff)