类型声明可以省略
final String a = 'final-string-dart';
final a = 'final-a-dart'
const String a = 'const-string-a';
const a = 'const-a';
初始后不能再赋值
final a = 'final-a';
a = 'abc';
const a = 'const-a';
a = 'abc';
不能和 var 同时使用
final var a = 'final-var-a';
const var a = 'const-var-a';
const 赋值 申明可省略
const List ls = const [11, 22, 33];
const List ls = [11, 22, 33];
区别
需要确定的值
final dt = DateTime.now();
const dt = const DateTime.now();
不可变性可传递
final List ls = [11, 22, 33];
ls[1] = 44;
const List ls = [11, 22, 33];
ls[1] = 44;
内存中重复创建
final a1 = [11 , 22];
final a2 = [11 , 22];
print(identical(a1, a2));
const a1 = [11 , 22];
const a2 = [11 , 22];
print(identical(a1, a2));
PDF文档整理:
Dart基础语言学习-上篇.pdf