Flutter For Loop 内的children只允许一个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 ),
}
],
),
),
)
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 ),
}
],
),
),
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将
HistoryView
和WeekWidget
包装在列中You can wrap
HistoryView
andWeekWidget
in a column使用扩展运算符
Use the spread operator