=> I want to print "Title" and "Value" in DataColumn
=> and there value want to print in DataRow
I want to print "key" as well as their "value" both in DataTable. First of all print "keys" in DataColumn and print "Values" in DataRow. This is Api Link that i used for calling "https://run.mocky.io/v3/23d7180b-7fba-4f31-a968-187f62ad68d9"
import 'dart:convert';
import "package:flutter/material.dart";
import 'package:http/http.dart' as http;
class HorizontalTable extends StatefulWidget {
HorizontalTable({Key key}) : super(key: key);
@override
State<HorizontalTable> createState() => _HorizontalTableState();
}
class _HorizontalTableState extends State<HorizontalTable> {
List<dynamic> ColumnsList = [];
List<dynamic> rowsList = [];
var decode;
@override
void initState() {
fetchStopEventData();
decode[0].forEach((key, value) {
print(key);
ColumnsList.add(
<DataColumn>[
DataColumn(label: Text('${key}')),
],
);
});
decode.forEach((dataRow) {
Map<String, DataCell> DataCellMap = {};
dataRow.forEach((key, value) {
print("$key : $value");
DataCellMap.addAll({key.toString(): DataCell(value)});
});
print("-------------------------------");
rowsList.add(<DataRow>[DataRow(cells: DataCellMap.values)]);
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: DataTable(
headingTextStyle: TextStyle(
fontWeight: FontWeight.normal,
fontSize: 12,
color: Colors.grey.shade600),
dataTextStyle: TextStyle(
fontWeight: FontWeight.normal,
fontSize: 12,
color: Colors.grey.shade600),
headingRowHeight: 25,
dataRowHeight: 25,
headingRowColor: MaterialStateColor.resolveWith(
(states) {
return Colors.grey.shade100;
},
),
dividerThickness: 1.0,
columnSpacing: 0.0,
horizontalMargin: 5.0,
showBottomBorder: false,
decoration: BoxDecoration(
border: Border.all(
width: 0.5,
color: Colors.grey.shade500,
)),
columns: ColumnsList,
rows: rowsList,
),
),
);
}
fetchStopEventData() async {
try {
final response = await http.get(
Uri.parse(
'https://run.mocky.io/v3/23d7180b-7fba-4f31-a968-187f62ad68d9'),
);
if (response.statusCode == 200) {
decode = jsonDecode(response.body.toString());
decode[0].forEach((key, value) {
print(key);
ColumnsList.add(key);
});
decode.forEach((dataRow) {
dataRow.forEach((key, value) {
print("Data Table: $key : $value");
});
print("-------------------------------");
});
} else {
throw Exception('Failed to load data');
}
} catch (e) {
print('Something went wrong : ${e.toString()}');
}
}
}
Here is an example: