zl程序教程

您现在的位置是:首页 >  工具

当前栏目

[Dart] final vs const

vs const Final Dart
2023-09-14 08:59:14 时间
void main() {
  var a = 1;
  print(a);

  int b = 2;
  print(b);

  final c = 'Hello';
  // c = 'Hello again'; // Uncomment to throw
  print(c);

  const d = 'World';
  print(d);
}

 

If we attempt to reset 'final' to a different value, we will get an error. The difference in practice between const and final is you use final when certain instance variables belong into classes.

 

final can be set in runtime, but const can only be set during compile time:

const int time = Date.now() // ERROR
final int time = Date.now() // GOOD