首页 > 解决方案 > 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']);
                    }),


                  ],
                ),
              ));
            });

有什么解决办法吗?谢谢!

标签: flutterdart

解决方案


事情是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'),
  ],
)

推荐阅读