Flutter For Loop inside children 只允许一个 widget

Flutter For Loop inside children only allows one widget

我在列表视图小部件中有一个 For 循环来填充其子项。现在我有一个小部件我的自定义历史视图小部件:

  Expanded(
    flex: 5,
    child: Container(
      width: double.infinity,
      color: Colors.white,
      child: ListView(
        padding: const EdgeInsets.only(top: 10.0),
        children: [
          for (var i = Provider.of<WeekList>(context)
                  .listOfWeeks
                  .toList()
                  .length;
              i > 1;
              i--)
            HistoryView(index: i - 2),
        ],
      ),
    ),
  )

问题是我想在历史视图小部件下添加第二个小部件,但我似乎无法弄清楚如何正确地执行此操作。我认为它会像这样简单:

      Expanded(
        flex: 5,
        child: Container(
          width: double.infinity,
          color: Colors.white,
          child: ListView(
            padding: const EdgeInsets.only(top: 10.0),
            children: [
              for (var i = Provider.of<WeekList>(context)
                      .listOfWeeks
                      .toList()
                      .length;
                  i > 1;
                  i--){
                      HistoryView(index: i - 2),
                      WeekWidget(index: i - 2 ),
                  }
                
            ],
          ),
        ),
      )

您可以将 HistoryViewWeekWidget 换成一列

使用展开运算符

      Expanded(
        flex: 5,
        child: Container(
          width: double.infinity,
          color: Colors.white,
          child: ListView(
            padding: const EdgeInsets.only(top: 10.0),
            children: [
              for (var i = Provider.of<WeekList>(context)
                      .listOfWeeks
                      .toList()
                      .length;
                  i > 1;
                  i--)
                      ...[
                      HistoryView(index: i - 2),
                      WeekWidget(index: i - 2 ),
                    ],                
            ],
          ),
        ),
      )