flutter - 列子项不能返回 null,但返回会停止循环
问题描述
我正在尝试遍历列表并为列表中的每个“步骤”生成一个小部件(文本)。
但是,在打印时,我得到了完全符合预期的错误Column's children must not contain any null values, but a null value was found at index 0
。
我明白,因为我没有返回任何东西。但是如果我确实返回了一些东西,循环就会停止,所以我只是迈出了第一步。
recipeMethodList(recipeMethod) {
for (var step in recipeMethod) {
stepNumber = stepNumber + 1;
Row(
children: <Widget>[
Text('$stepNumber. $step'),
],
);
print('$stepNumber. $step');
}
我称之为:
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[recipeMethodList(recipeMethod)],
),
解决方案
您可以尝试更改功能,例如
List<Widget> recipeMethodList(List recipeMethod) {
int stepNumber=0;
return recipeMethod.map((step) {
stepNumber = stepNumber + 1;
return Text('$stepNumber. $step');
}).toList();
}
推荐阅读
- python - Keras 层构造函数名称参数用法
- javascript - 如何在 vuejs 中的 json 对象中找到嵌套键
- python - 创建没有集群的自定义热图
- c - 在通过对函数的引用传递结构时,需要使用 & 运算符吗?
- google-analytics - GA4 (Google Analytics 4) - 有没有办法获取既是入口又是出口的 url 列表?
- javascript - 如何从拥有它的每个人中删除特定角色 Discord.js v12
- javascript - 在 Angular 中按 Tab 时聚焦下一个输入
- python - 如何通过将函数应用于数据框中的每一行来创建新列?
- rust - 使用锈柴油时如何实现小于查询
- nginx - Openresty:如何使用不同的 nginx.conf?