Can someone explain to me how/when/why to use const
keyword, or it is just "a way to declare a constant variable"? If so, what's the difference between this :
int x = 5;
and
const int x = 5;
Could you guys please give me an example?
The const keyword is used when the value of the variable is known at compile-time and never changes. In other words, the compiler knows in advance what value is to be stored in that variable. const int x = 1;//At compile time, the value of x is going to be 1 and will not change.
The const keyword specifies that a variable's value is constant and tells the compiler to prevent the programmer from modifying it.
It is recommended to use const constructors whenever possible when creating Flutter widgets. The reason is the performance increase, since Flutter can save some calculations by understanding that it can reuse that widget from a previous redraw in the current one, since it is a constant value.
Final and const A final variable can be set only once; a const variable is a compile-time constant. (Const variables are implicitly final.) Note: Instance variables can be final but not const .
const
means compile time constant. The expression value must be known at compile time. const
modifies "values".
From news.dartlang.org,
"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.
if you use
const x = 5
then variable x can be used in a cosnt collection like
const aConstCollection = const [x];
if you don't use const
, and just use x = 5
then
const aConstCollection = const [x];
is illegal.
More examples from www.dartlang.org
class SomeClass {
static final someConstant = 123;
static final aConstList = const [someConstant]; //NOT allowed
}
class SomeClass {
static const someConstant = 123; // OK
static final startTime = new DateTime.now(); // OK too
static const aConstList = const [someConstant]; // also OK
}
Here are some facts about const
values:
The value must be known at compile time.
const x = 5; // OK
Anything that is calculated at runtime can't be const
.
const x = 5.toDouble(); // Not OK
A const
value means that it's deeply constant, that is, every one of its members is constant recursively.
const x = [5.0, 5.0]; // OK
const x = [5.0, 5.toDouble()]; // Not OK
You can create const
constructors. That means that it is possible to create const
values from the class.
class MyConstClass {
final int x;
const MyConstClass(this.x);
}
const myValue = MyConstClass(5); // OK
const
values are canonical instances. That means that there is only a single instance no matter how many you declare.
main() {
const a = MyConstClass(5);
const b = MyConstClass(5);
print(a == b); // true
}
class MyConstClass {
final int x;
const MyConstClass(this.x);
}
If you have a class member that is const
, you must also mark it as static
. static
means it belongs to the class. Since there is only ever one instance of const
values, it wouldn't make sense for it to not be static
.
class MyConstClass {
static const x = 5;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With