我正在尝试使用flutter的DataTable小部件,但一直出现此错误,我想创建一种方法来为表格生成数据。
var dataList = [
DataSectionCollection(
category: 'Category',
date: 'January 01, 2019',
item: [
DataSectionItem(
symbol: "ICMS", amount: " 474.858.228.17", percentage: 3.55),
DataSectionItem(
symbol: "ICMS", amount: " 474.858.228.17", percentage: 3.55),
DataSectionItem(
symbol: "ICMS", amount: " 474.858.228.17", percentage: 3.55),
DataSectionItem(
symbol: "ICMS", amount: " 474.858.228.17", percentage: 3.55),
],
),
DataSectionCollection(
category: 'Category',
date: 'January 01, 2019',
item: [
DataSectionItem(
symbol: "AAAA", amount: " 474.858.228.17", percentage: 3.55),
DataSectionItem(
symbol: "AAA", amount: " 474.858.228.17", percentage: 3.55),
DataSectionItem(
symbol: "AAA", amount: " 474.858.228.17", percentage: 3.55),
DataSectionItem(
symbol: "AAA", amount: " 474.858.228.17", percentage: 3.55),
],
),
DataSectionCollection(
category: 'Category',
date: 'January 01, 2019',
item: [
DataSectionItem(
symbol: "BBBB", amount: " 474.858.228.17", percentage: 3.55),
DataSectionItem(
symbol: "BBBB", amount: " 474.858.228.17", percentage: 3.55),
DataSectionItem(
symbol: "BBBB", amount: " 474.858.228.17", percentage: 3.55),
DataSectionItem(
symbol: "BBBB", amount: " 474.858.228.17", percentage: 3.55),
],
),
];
_getData01(List listOfData) {
return DataTable(
columns: listOfData
.map(
(column) => DataColumn(
label: Container(),
),
)
.toList(),
rows: listOfData
.map((stat) => stat.item.map((row) => DataRow(cells: [
DataCell(
Text(row.symbol),
),
DataCell(
Text(row.amount),
),
DataCell(
Text("${row.symbol}"),
),
])))
.toList(),
);
}
我不确定自己在做什么错。有人可以帮忙吗?我在列表上映射以创建列的第一位到第二位都工作正常。
您的问题在于.map()遍历数据时方法的嵌套。我将其弄乱以使其更具可读性并交换为.forEach()方法:
Widget _getData01(List listOfData) {
List<DataRow> rows = [];
listOfData.forEach((stat){
stat.item.forEach((row){
rows.add(
DataRow(
cells: [
DataCell(
Text(row.symbol),
),
DataCell(
Text(row.amount),
),
DataCell(
Text("${row.symbol}"),
),
]
)
);
});
});
return DataTable(
columns: listOfData.map(
(column) => DataColumn(
label: Container(),
)
).toList(),
rows: rows,
);
}
我只能通过用自己的数据替换您的模型类来进行测试,因此请进行测试并提供反馈。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。