编辑 :
从Dart 2.2开始,新语法原生支持此功能:
Column( children: [ if (foo != null) Text(foo), Bar(), ],);
这个问题目前正在讨论在github
这里。
但是现在,您可以使用dart
sync*函数:
Row( children: toList(() sync* { if (foo == 42) { yield Text("foo"); } }),);在哪里
toList:
typedef Iterable<T> IterableCallback<T>();List<T> toList<T>(IterableCallback<T> cb) { return List.unmodifiable(cb());}它不仅解决了条件加法问题;多亏了,它还允许“传播算子”
yield*。例:
List<Widget> foo;Row( children: toList(() sync* { yield Text("Hello World"); yield* foo; }),);


