1. const 值必须在编译时知道,初始化后无法更改
比如: const time = '2020-01-01'。
const time = DateTime.now() // 会报错 因为我们无法将运行时值分配给 const 变量
2. final 值必须在运行时知道,初始化后无法更改
final time = getTime()。
final time = DateTime.now() // 成功
作者:PHP的点滴
链接:https://www.jianshu.com/p/e1e29a64f146
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
补充,在flutter中,const还有如下的作用
By calling with the const keyword, the widget does not rebuild when other widgets change
their state in the tree. If you omit the const keyword, the widget will be called every time the parent
widget redraws.
本文介绍了Flutter中const和final关键字的区别。const值必须在编译时确定且不可更改,例如常量字符串;而final值可以在运行时赋值,一旦赋值后同样不能修改。在Flutter中,使用const修饰的Widget可以避免不必要的重建,提高性能。
701

被折叠的 条评论
为什么被折叠?



