简体   繁体   English

Dart flutter if-else 体属性

[英]Dart flutter if-else body property

I did the Write Your First Flutter App, part 2 tutorial.我完成了编写您的第一个 Flutter 应用程序,第 2 部分教程。

Let's consider this code, extracted from the application of the tutorial.让我们考虑从教程的应用程序中提取的这段代码。

return Scaffold(
            appBar: AppBar(
              title: Text('Saved Words'),
            ),
            body: ListView(children: divided),
          );

Why can't I write something like this:为什么我不能写这样的东西:

return Scaffold(
            appBar: AppBar(
              title: Text('Saved Words'),
            ),
            body: () { if (true) {
              ListView(children: divided);
            }
            },
          );

Can't I put an If statement while I am defining a Widget property?我不能在定义 Widget 属性时添加 If 语句吗?

You cannot.你不能。 But have a look at the code below, this allows you to do essentially the same.但是看看下面的代码,这可以让你做同样的事情。

  return Scaffold(
        appBar: AppBar(
        title: Text('Saved Words'),
        ),
        body: variable == 2 ? Container() : Center()
 );

Edit for clarification, the syntax goes:为澄清而编辑,语法如下:

bool evaluation?布尔评估? if true: if false如果为真:如果为假

You can use ternary operator:您可以使用三元运算符:

body: _isTrue
        ? ListView(children: divided)
        : <some other widget>;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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