flutter - Flutter - 如何在 ListView 构建器中列出 forEach() 值?
问题描述
您好我正在尝试将数据从 JSON 列出到 Listview Builder 。但是 Flutter 给了我这个错误:Column's children must not contain any null values, but a null value was found at index 0
我试图在列表视图中像这样映射每个
alo[Index]['rewards'].forEach((k, v) {
Text(v['name']);
}),
这是我的完整代码:
shrinkWrap: true,
itemCount: alo.length,
itemBuilder: (BuildContext ctxt, int Index) {
return Card(
child: Padding(
padding: const EdgeInsets.all(15.0),
child: Column(
children: <Widget>[
alo[Index]['rewards'].forEach((k, v) {
Text(v['name']);
}),
],
),
));
});
有什么解决办法吗?谢谢!
解决方案
事情是forEach()
不返回任何东西,你可以使用map()
,像这样:
children: alo[Index]['rewards'].values.map<Widget>((v) => Text(v['name'])).toList()
如果要添加更多小部件,可以执行以下操作:
Column(
children: <Widget>[
Column(
children: alo[Index]['rewards']
.values
.map<Widget>((v) => Text(v['name']))
.toList(),
),
Text('Other widget'),
],
)
推荐阅读
- python - “类名”对象在递归函数中没有属性“函数名”
- spring - Spring boot 错误 ORA-01400: 无法将 NULL 插入 ("AAKASH"."USERS"."EMAILVERIFICATIONSTATUS")
- javascript - 选择父元素,然后选择下一个兄弟元素
- shiny - 在 Shiny 中重命名 DT 中的列名
- swift - 字符中的 == 是如何定义的?
- c - 新手 C 程序员遇到字符串和 malloc 问题
- c++ - 文件未创建,但可以工作
- node.js - 我可以通过 nodemailer 使用 express 发送多少封邮件?
- python - 如何知道我使用的是哪个版本的 pandas-profiling?
- json - 在 Biztalk 中将 XML Schema 转换为 JSON 数组列表