I am Creating a Dynamic data table view in flutter , where the column headers and the row values are added dynamically. The data is a Json response,the header is a string array and the data to be displayed in row is also a string array .The goal is to display the data in the table view without any hardcoded values.
This is how I have tried is to display the data.
return ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.vertical,
itemCount: snapshot.data.dataList.length,
itemBuilder: (BuildContext context, int index){
return SingleChildScrollView(
child: DataTable(
columns: (snapshot.data.headerList[index] as List).map((item) =>
DataColumn(
label:(
List.generate(item.length,(index){
return Text(item[index].toString());
})
)
)).toList(),
rows: (snapshot.data.dataList[index].dataList as List).map((item) =>
DataRow(
cells:<DataCell>[
DataCell(
List.generate(item.length,(index){
return Text(item[index].toString());
})
)
])).toList(),
),
This is the JSON response
"DayEnd": {
"ColumnWidths": "40′168′96′96′108′156",
"Headers": "SL.>′Customer< ′Balance Qty>′Amount>′Oldest / Recent ",
"FieldSeparator": "′",
"DataList": [
{
"Data": "1. ′ABD ′14 / 14.60′11,090′313 / 313",
"NextLevelZoomData": [
{
"Element": "eg7P27fbW/GmCr"
},
{
"Element": "AAA=="
}
],
"NextLevelZoomType": 2
},
{
"Data": "2. ′LATA′16′7,921′20 / 9",
"NextLevelZoomData": [
{
"Element": "MT63z0m7piukmtJZqdZ"
},
{
"Element": "QT/2zE/AAA="
}
],
"NextLevelZoomType": 2
},
{
"Data": "3. ′ANAND′11′915′426 / 426",
"NextLevelZoomData": [
{
"Element": "AAA="
}
],
"NextLevelZoomType": 2
},
{
"Data": "4. ′Asts′1′1,010′27 / 27",
"NextLevelZoomData": [
{
"Element": "w/VbTHwKgQAAA=="
}
],
"NextLevelZoomType": 2
},
}
The above response gives a string data Headers
which is separated by a special character "`",the each character has to be displayed in a each column header so which is converted to an array. Similarly the data as well . The column width that has to be maintained for each data is mentioned in columnWidth
Similarly the alignment of the data like if "SL.>" then ">" it has to be right aligned.
I am not sure if this is the right approach to get it, This gives an error
The argument type 'List<Text>' can't be assigned to the parameter type 'Widget'.
Any idea to approach in a better way will be much appreciated.
question from:
https://stackoverflow.com/questions/65856234/dynamically-display-json-data-in-data-table-layout-in-flutter