rozerro
rozerro

Reputation: 7216

If a widget declared const, does it spread to its child?

Are there any practical difference between these two?

If a widget declared const, does it spread to it child?

Container(
    height: tabHeight,
    child: const Tab(
      icon: Icon(Icons.settings),
    ),
),

vs

Container(
    height: tabHeight,
    child: const Tab(
      icon: const Icon(Icons.settings),
    ),
),

Upvotes: 0

Views: 57

Answers (2)

timilehinjegede
timilehinjegede

Reputation: 14053

You can omit the const keyword when initialising expressions of a const declaration. Below is what the official documentation says:

The const keyword isn’t just for declaring constant variables. You can also use it to create constant values, as well as to declare constructors that create constant values. Any variable can have a constant value.

var foo = const []; You can omit const from the initializing expression of a const declaration. For details, see DON’T use const redundantly.

foo = [1, 2, 3]; // Was const []

I hope this helps.

Upvotes: 0

Rémi Rousselet
Rémi Rousselet

Reputation: 277567

Yes The const keyword is optional when obvious.

For example, we don't have to write:

const example = const Something(const Other());

We can just write:

const example = Something(Other());

Upvotes: 2

Related Questions