< p > 常量:
如果你拥有的值是在运行时计算的(例如new DateTime.now()),你可以const a = 1;3对它使用const。然而,如果该值在编译时已知(const a = 1;),则应该使用const而不是final。在const和final之间还有两个很大的区别。首先,如果你在类中使用const,你必须将它声明为static const而不仅仅是const。其次,如果你有一个const集合,它里面的所有东西都在const中。如果你有一个final集合,它里面的所有东西都是const a = 1;3 final.
< p > 最后:
如果你在编译时不知道final的值,应该在const上使用,并且它将在运行时计算/获取。如果你想要一个不可更改的HTTP响应,如果你想从数据库中获取一些东西,或者如果你想从本地文件中读取,请使用final。编译时不知道的任何东西都应该是final / const.
const a = 5;
// Uncommenting below statement will cause compile time error.
// Because we can't able to assign a runtime value to a const variable
// const b = DateTime.now();
应该是在同一行初始化。
const a = 5;
// Uncommenting below 2 statement will cause compilation error.
// Because const variable must be initialized at the same line.
// const b;
// b = 6;
以下所有陈述均可接受。
// Without type or var
const a = 5;
// With a type
const int b = 5;
// With var
const var c = 6;
类级别的const变量应该像下面这样初始化。
Class A {
static const a = 5;
}
实例级const变量不可用。
Class A {
// Uncommenting below statement will give compilation error.
// Because const is not possible to be used with instance level
// variable.
// const a = 5;
}
Class A {
final a, b;
const A(this.a, this.b);
}
void main () {
// There is no way to change a field of object once it's
// initialized.
const immutableObja = const A(5, 6);
// Uncommenting below statement will give compilation error.
// Because you are trying to reinitialize a const variable
// with other value
// immutableObja = const A(7, 9);
// But the below one is not the same. Because we are mentioning objA
// is a variable of a class A. Not const. So we can able to assign
// another object of class A to objA.
A objA = const A(8, 9);
// Below statement is acceptable.
objA = const A(10, 11);
}
我们可以使用Const关键字到列表。
const a = const[] -变量a初始化为const,其中包含const对象的列表,列表应该只包含编译时间常量和不可变对象)。所以我们不能用另一个列表来赋值a。
var a = const[] -变量a初始化为__ABC1,其中包含一个列表const对象. var a = const []所以我们可以将另一个列表赋值给变量a。
Class A {
final a, b;
const A(this.a, this.b);
}
class B {
B(){ // Doing something }
}
void main() {
const constantListOfInt = const [5, 6, 7,
// Uncommenting below statement give compilation error.
// Because we are trying to add a runtime value
// to a constant list
// DateTime.now().millisecondsSinceEpoch
];
const constantListOfConstantObjA = const [
A(5, 6),
A(55, 88),
A(100, 9),
];
// Uncommenting below 2 statements will give compilation error.
// Because we are trying to reinitialize with a new list.
// constantListOfInt = [8, 9, 10];
// constantListOfConstantObjA = const[A(55, 77)];
// But the following lines are little different. Because we are just
// trying to assign a list of constant values to a variable. Which
// is acceptable
var variableWithConstantList = const [5, 6, 7];
variableWithConstantList = const [10, 11, 15];
var variableOfConstantListOfObjA = const [A(5, 8), A(7, 9), A(10, 4)];
variableWithConstantList = const [A(9, 10)];
}
最后:
final关键字也用于将变量设置为保持恒定值。一旦初始化,就不能更改值。
final a = 5;
// Uncommenting below statement will give compilation error.
// Because a is declared as final.
// a = 6;
以下所有陈述均可接受。
// Without type or var
final a = 5;
// With a type
final int b = 5;
// Can't use var along with final keyword. Uncommenting below line cause compilation issue.
// final var c = 6;
能够分配运行时值。
// DateTime.now() will return the time when the line is getting
// executed. Which is a runtime value.
final a = DateTime.now();
var b = 5;
final c = b;
类级最终变量必须在同一行中初始化。
Class A {
static final a = 5;
static final b = DateTime.now();
}
实例级final变量必须在同一行或在构造函数初始化中初始化。该值将在创建对象时被放入内存中。
Class A {
final a = 5;
}
// Constructor with a parameter.
Class B {
final b;
B(this.b);
}
// Constructor with multiple parameter.
Class C {
final c;
C(this.c, int d) {
// Do something with d
}
}
void main() {
A objA = new A();
B objB = new B(5);
C objC = new C(5, 6);
}
分配一个列表。
final a = [5, 6, 7, 5.6, A()];
// Uncommenting Below statement will give compilation error.
// Because we are trying to reinitialize the object with another list.
// a = [9.9, 10, B()];
final myConst = 1;
const myFinal = 2;
final a = myConst; // possible
final b = myFinal; // possible
const c = myConst; // this is not possible
const d = myFinal; // possible
void main() {
const sum = 1 + 2;
// ✅ const can not change its value
print("sum = ${sum}");
// ⚠️ Const variables must be initialized with a constant value.
const time = new DateTime.now();
// ❌ Error: New expression is not a constant expression.
print("time = ${time}");
}
最后
// new DateTime.now();
// dynamic timestamp
void main() {
final sum = 1 + 2;
// ✅ final can not change its value
print("sum = ${sum}");
final time = new DateTime.now();
// ✅ final === var with fixed value
print("time = ${time}");
}