gpt4 book ai didi

flutter - 如何在Flutter中映射动态列表

转载 作者:行者123 更新时间:2023-12-03 04:19:58 26 4
gpt4 key购买 nike

好的,所以我有点陌生,但是我尝试使用map获取和显示列表中的项目。

class TransactionTable extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return TransactionTableState();
}
}

class TransactionTableState extends State {
final List moneyTransactions = [
new MoneyTransactionModel(
id: "1",
productId: "lime",
entryType: "sold",
quantity: "60kg",
unitPrice: "240rwf/kg",
paidBy: "mtn",
createdAt: "2020-04-20T00:15:08.932Z"),
new MoneyTransactionModel(
id: "2",
productId: "lime",
entryType: "sold",
quantity: "60kg",
unitPrice: "240rwf/kg",
paidBy: "mtn",
createdAt: "2020-04-20T00:15:08.932Z"),
new MoneyTransactionModel(
id: "3",
productId: "lime",
entryType: "sold",
quantity: "60kg",
unitPrice: "240rwf/kg",
paidBy: "mtn",
createdAt: "2020-04-20T00:15:08.932Z"),
new MoneyTransactionModel(
id: "4",
productId: "lime",
entryType: "sold",
quantity: "60kg",
unitPrice: "240rwf/kg",
paidBy: "mtn",
createdAt: "2020-04-20T00:15:08.932Z"),
];

Widget build(BuildContext context) {
// return
return Column(
children: <Widget>[
moneyTransactions.map((e) => MyListTile()); // i need to the the loop here
],
);
}
}
MyListTile是应该在 moneyTransactions列表中显示一项的小部件

最佳答案

您正在将列表插入现有的List中,您的构建方法应如下所示:

Widget build(BuildContext context) {
return Column(
children: moneyTransactions.map((e) => MyListTile()).toList();
);
}
现在,对于 List中的每个项目,将生成一个 MyListTile实例。
要实际访问 MoneyTransaction的信息,您必须将对象作为参数传递给 MyListTile的构造函数。

关于flutter - 如何在Flutter中映射动态列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63264902/

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