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.