简体   繁体   English

Flutter For Loop inside children only allows one widget

[英]Flutter For Loop inside children only allows one widget

I have a For loop inside a listview widget to populate its children.我在列表视图小部件内有一个 For 循环来填充其子项。 Right now I have one widget my custom history view widget:现在我有一个小部件我的自定义历史视图小部件:

  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),
        ],
      ),
    ),
  )

The problem is I want to add a second widget under the History view widget, but I cant seem to figure out how to correctly do this.问题是我想在历史视图小部件下添加第二个小部件,但我似乎无法弄清楚如何正确地执行此操作。 I thought it would be as easy as this:我认为它会像这样简单:

      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 ),
                  }
                
            ],
          ),
        ),
      )

You can wrap HistoryView and WeekWidget in a column您可以将HistoryViewWeekWidget包装在一列中

Use the spread operator使用扩展运算符

      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 ),
                    ],                
            ],
          ),
        ),
      )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM