简体   繁体   中英

Flutter For Loop inside children only allows one widget

I have a For loop inside a listview widget to populate its children. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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