在Dart中,const和final关键字之间有什么区别?
当前回答
Const表示它的初始值必须是固定的,不能是动态值;
Final意味着它的初始值必须是固定的,但可以是一个动态值,等于具有固定值的var。
代码演示
常量
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}");
}
截图
refs
https://dart.dev/guides/language/language-tour#final-and-const
其他回答
你不能使用final对象初始化const对象。例如:
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
所有这些答案我都可以用简洁的方式描述。
const list = [1, 2, 3];
变量/标识符和值都是const。Like - const list = const [1,2,3] 这就是为什么他们不允许被重新分配。 很适合全局变量。 可以将其用作类变量,但必须设置为静态。Like - static const list =[1,2,3]。
vs:
final list = [1, 2, 3];
变量/标识符是const,而值不是。Like - const list = [1,2,3] 这就是为什么我们可以执行like - list.add(4)
dart中的Final和const很容易混淆,以至于我们认为它们是相同的。
让我们来看看它们的区别:
附注:我包括图像而不是文本,因为我不能将信息制成表格 在Stackoverflow .md格式轻松。
dart的网站上有一个帖子,解释得很好。
最后:
"final"表示单赋值:final变量或字段必须有初始化式。一旦赋值,最终变量的值就不能更改。Final修改变量。
常量:
“const”在Dart中有一个更复杂和微妙的含义。Const用于修改值。你可以在创建集合时使用它,比如const[1,2,3],也可以在构造对象(而不是new)时使用它,比如const Point(2,3)。在这里,const意味着对象的整个深度状态可以完全在编译时确定,对象将被冻结并且完全不可变。
Const对象有几个有趣的属性和限制:
它们必须从可以在编译时计算的数据中创建。const对象不能访问运行时计算所需的任何内容。1 + 2是一个有效的const表达式,但new DateTime.now()不是。
它们是深刻的、传递的不可变的。如果final字段包含一个集合,则该集合仍然可以是可变的。如果你有一个const集合,它里面的所有东西也必须是const,递归地。
它们被规范化了。这有点像字符串实习:对于任何给定的const值,无论对const表达式求值多少次,都会创建一个const对象并重新使用。
那么,这意味着什么呢?
Const: If the value you have is computed at runtime (new DateTime.now(), for example), you can not use a const for it. However, if the value is known at compile time (const a = 1;), then you should use const over final. There are 2 other large differences between const and final. Firstly, if you're using const inside a class, you have to declare it as static const rather than just const. Secondly, if you have a const collection, everything inside of that is in const. If you have a final collection, everything inside of that is not final.
最后: 如果在编译时不知道Final的值,则应该在const上使用Final,并且它将在运行时计算/获取。如果你想要一个不可更改的HTTP响应,如果你想从数据库中获取一些东西,或者如果你想从本地文件中读取,请使用final。编译时不知道的任何东西都应该在const上使用final。
话虽如此,const和final都不能被重赋,但final对象中的字段,只要它们本身不是const或final,就可以被重赋(不像const)。
Const表示它的初始值必须是固定的,不能是动态值;
Final意味着它的初始值必须是固定的,但可以是一个动态值,等于具有固定值的var。
代码演示
常量
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}");
}
截图
refs
https://dart.dev/guides/language/language-tour#final-and-const