
上QQ阅读APP看书,第一时间看更新
final and const
A variable will never intend to change its value after it is assigned, and you can use the final and const ways for declaring this:
final value = 1;
The value variable cannot be changed once it's initialized:
const value = 1;
Just like the final keyword, the value variable cannot be changed once it's initialized, and its initialization must occur together with a declaration.
In addition to this, the const keyword defines a compile-time constant. As a compile-time constant, the const values are known at compile time. They also can be used to make object instances or Lists immutable, as follows:
const list = const [1, 2, 3]
// and
const point = const Point(1,2)
This will set the value of both variables during compile time, turning them into completely immutable variables.