Devilsen
Devilsen

Reputation: 63

In Dart, what's the difference between 'const' parameter?

padding: const EdgeInsets.all(25.0)
padding: EdgeInsets.all(25.0),

In Dart demo, most of padding or child add const, is there any optimization?

https://docs.flutter.io/flutter/widgets/Padding-class.html

Upvotes: 6

Views: 2060

Answers (3)

Mohan
Mohan

Reputation: 1244

Let us consider we have these three lines of code:

1.const EdgeInsets.all(25.0)
2.const EdgeInsets.all(25.0)
3.const EdgeInsets.all(25.0)

1. At first line EdgeInsets class creates a new object and assigns its values for left, top, right, bottom and render the given widget and then it creates a constant object with same value for rendering if it is found in another place.

2. Hey there is already an object with this value, so just render it.

3. Hey there is already an object with this value, so just render it.

Now, let us consider these scenario:

1.EdgeInsets.all(25.0)
2.EdgeInsets.all(25.0)
3.EdgeInsets.all(25.0)

1. At first line, EdgeInsets class creates a new object and assigns its values for left, top, right, bottom and render the given widget.

2. At the second line, EdgeInsets class creates a new object and assigns its values for left, top, right, bottom and render the given widget.

3. At third line, EdgeInsets class creates a new object and assigns its values for left, top, right, bottom and render the given widget.

So, by using const we can reduce the time for recreating the same object every time and using it, instead, we create an object once and then reuse it at every time we need.

Upvotes: 9

CopsOnRoad
CopsOnRoad

Reputation: 267384

const means that the object's (padding here) entire deep state can be determined entirely at compile time and that the padding will be frozen and completely immutable.

PS: Please read comment for better explanation

Upvotes: 0

Andrii Turkovskyi
Andrii Turkovskyi

Reputation: 29438

"const" has a meaning that's a bit more complex and subtle in Dart. const modifies values. You can use it when creating collections, like const [1, 2, 3], and when constructing objects (instead of new) like const Point(2, 3). Here, const means that the object's entire deep state can be determined entirely at compile time and that the object will be frozen and completely immutable.

Const objects have a couple of interesting properties and restrictions: They must be created from data that can be calculated at compile time. A const object does not have access to anything you would need to calculate at runtime. 1 + 2 is a valid const expression, but new DateTime.now() is not. They are deeply, transitively immutable. If you have a final field containing a collection, that collection can still be mutable. If you have a const collection, everything in it must also be const, recursively. They are canonicalized. This is sort of like string interning: for any given const value, a single const object will be created and re-used no matter how many times the const expression(s) are evaluated. In other words:

Src here

Upvotes: 1

Related Questions