I did the Write Your First Flutter App, part 2 tutorial.

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?


Solution 1: Benedikt J Schlegel

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


Solution 2: GrahamD

You can use ternary operator:

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