taylorren
6/17/2018 - 12:51 AM

`final` and 'const`

final and 'const`

void main() {
  final name = 'Bob'; // Without a type annotation
  // name = 'Alice';  // 取消这行注释就出错了
  final String nickname = 'Bobby';
  
  print(name);
  print(nickname);
  
  // Note: [] creates an empty list.
  // const [] creates an empty, immutable list (EIL).
  var foo = const []; // foo is currently an EIL.
  final bar = const []; // bar will always be an EIL.
  const baz = const []; // baz is a compile-time constant EIL.
  print(foo);
  print(bar);
  print(baz);
  // You can change the value of a non-final, non-const variable,
  // even if it used to have a const value.
  foo = [1,2,'List'];
  print(foo);

  // You can't change the value of a final or const variable.
  // bar = []; // Unhandled exception.
  // baz = []; // Unhandled exception.
}