Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 惯用Dart:使用可选组件显式定义列表?_Flutter_Dart - Fatal编程技术网

Flutter 惯用Dart:使用可选组件显式定义列表?

Flutter 惯用Dart:使用可选组件显式定义列表?,flutter,dart,Flutter,Dart,在Dart/Flatter中,行小部件可以这样编写: Row( children: <Widget>[ Text('a'), Text('b'), ], ) bool notNull(Object o) => o != null; bool condition = ...; Row( children: <Widget>[ Text('a'), condition ? Text('b') : null, ].w

在Dart/Flatter中,行小部件可以这样编写:

Row(
  children: <Widget>[
    Text('a'),
    Text('b'),
  ],
)
bool notNull(Object o) => o != null;

bool condition = ...; 
Row(
  children: <Widget>[
    Text('a'),
    condition ? Text('b') : null, 
  ].where(notNull).toList(),
)
如果仅在满足条件的情况下才添加第二个文本,那么如何改变这种情况? 它可以是:

bool condition = ...;
List<Widget> rowComponent = [Text('a')];
if(condition) {
  rowComponent.add(Text('b'));
}

Row(
  children: rowComponent,
);
有没有一种更地道、更不冗长的方式?比如:

bool condition = ...; 
Row(
  children: <Widget>[
    Text('a'),
    condition ? Text('b') : Container(), 
  ],
)
这确实有效,但仅在颤振环境中有效,其中容器可以用作空对象并创建此未使用的容器。有更好的办法吗


谢谢

这样怎么样?也许你喜欢这个

你不需要使用容器


参考这一点:

我做了如下工作:

Row(
  children: <Widget>[
    Text('a'),
    Text('b'),
  ],
)
bool notNull(Object o) => o != null;

bool condition = ...; 
Row(
  children: <Widget>[
    Text('a'),
    condition ? Text('b') : null, 
  ].where(notNull).toList(),
)

如果您有一个方法可以创建并返回一个小部件,或者返回null,那么这也很有用。

正是我想要的!谢谢!