gpt4 book ai didi

android - Flutter List 应该是 List

转载 作者:行者123 更新时间:2023-11-29 05:10:00 24 4
gpt4 key购买 nike

我正在尝试使用 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,
);
}

我只能通过用我自己的数据替换您的模型类来测试这一点,所以请测试它并给我反馈。

关于android - Flutter List<Dynamic> 应该是 List<DataRow>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59777679/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com