在Javascript中我会使用扩展运算符:
现在我和Flutter有同样的问题:
Widget build(BuildContext context) {
return Column(
children: <Widget>[
MyHeader(),
_buildListOfWidgetForBody(), // <- how to spread this <Widget>[] ????
MyCustomFooter(),
],
);
}
你现在可以用Dart 2.3进行传播
var a = [0,1,2,3,4];
var b = [6,7,8,9];
var c = [...a,5,...b];
print(c);
你没有,at the moment。但是你可以从中构建一个List
并随时插入/添加其他元素。
Widget build(BuildContext context) {
final List<Widget> columnWidgets = List.from(_buildListOfWidgetForBody())
..insert(0, MyHeader())
..add(MyCustomFooter());
return Column(
children: columnWidgets
);
}
从Dart 2.3发布以来,您现在可以使用扩展运算符。
List<int> a = [0,1,2,3,4];
List<int> b = [6,7,8,9];
List<int> c = [...a,5,...c];
将此添加到dart https://github.com/dart-lang/language/issues/47的未来版本中存在问题
但是现在你可以使用sync*
和yield*
Iterable<Widget> _buildChildren sync* {
yield MyHeader();
yield* _buildListOfWidgetForBody();
yield MyCustomFooter();
}
编辑:从Dart 2.3开始,您现在可以:
Widget build(BuildContext context) {
return Column(
children: <Widget>[
MyHeader(),
..._buildListOfWidgetForBody(),
MyCustomFooter(),
],
);
}