Toujo
Toujo

Reputation: 335

How to add row element dynamically by calling API in flutter?

**My problem is I want to keep adding data in my row by calling My API, instead of updating it **

Image

 SingleChildScrollView(
        scrollDirection: Axis.horizontal,
        child: DataTable(
          columnSpacing: 13.0,
          columns: <DataColumn>[
            DataColumn(label: Text("TestName")),
            DataColumn(label: Text("Fee")),
            DataColumn(label: Text("Discounted Fee")),
            DataColumn(label: Text("Booking Fee")),
          ],
         rows: <DataRow>[
          DataRow(
             cells: <DataCell>[
            DataCell(Container(child: Text(testName?? '',overflow: TextOverflow.ellipsis))),
            DataCell(Text(getTestFeeObj?.fee ?? '')),
            DataCell(Text(getTestFeeObj?.discountedFee ?? '')),
            DataCell(Text(getTestFeeObj?.bookingFee ?? '')),
           ],
          )
         ]
         ),
      )

Upvotes: 0

Views: 512

Answers (1)

Agnaramon
Agnaramon

Reputation: 881

You can collect your API data in an array, add new data to it and use map function to construct your DataRow this way:

// your array data
var datas = [...]

// add new data to it
setState((){
  datas.add(...);
});

// use them for your DataRow
DataTable(
  row: datas.map((data){
    return DataRow(
       cells:[
         // your cells
         DataCell(Text(data.fieldValue))
       ]
    );
  }).toList()
)
 

Upvotes: 1

Related Questions