首页 > 解决方案 > 列子项不能返回 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)],
            ),

标签: flutterdart

解决方案


您可以尝试更改功能,例如

List<Widget> recipeMethodList(List recipeMethod) {
  int stepNumber=0;
  return recipeMethod.map((step) {
    stepNumber = stepNumber + 1;
    return Text('$stepNumber. $step');
  }).toList();
}

推荐阅读