[官方資料](https://dart.dev/language/variables)
## var
var具有推斷類型的能力, `var a=23;`, 則變數a的類型是int, 並且之後都不能更改a的類型
var a=23;
a=46;
// a='bbb'; // value of type 'String' can't be assigned to a variable of type 'int'.
如果var宣告時沒有賦值, 則其類型是可變, 此時與Object? 或 dynamic等價
var a; // 宣告時沒有確定類型
a=46;
a='bbb';
a=true;
local variable 使用var宣告, 而不是明確宣告類型
## dynamic
dynamic宣告的變數在編譯時不進行類型檢查, 運行時也可以動態確認類型
dynamic value=true;
print(value.runtimeType);
value = 'Hello';
print(value.runtimeType);
value = 123;
print(value.runtimeType);
## Object
可以賦值任意型別, 其後也可以任意更改, 但不可為null(non-null variable)
Object a=23; // non-nullable宣告時就要賦值
print(a); // 23
a = 'string';
print(a); // string
a = true;
print(a); // true
## 明確聲明類型
Null a;
print(a.runtimeType); // null
print(a); // null
num d = 12.5;
print(d.runtimeType);
int b = 23;
print(b.runtimeType);
String c = 'aaa';
print(c.runtimeType);
bool e = false;
print(e.runtimeType);
## Final and const
#### 如果從頭到尾不會改變的常量, 可以使用const宣告
const pi = 3.14159; // 可省略類型
const double atm = 1.01325 * bar;
var a=const[1,2,3];
// a[3]=4; // Cannot modify an unmodifiable list
print(a[0]);
// a=456; // A value of type 'int' can't be assigned to a variable of type 'List<int>'.
a=[4,5,6]; // a本身不是常量變數
print(a); // [4,5,6]
const b=123; // b是常量變數
// b=456; // Can't assign to the const variable 'b'.
final c=const[7,8,9];
實例變數可以是final不能是const, 如果要用const, 必須宣告成 static const(類的常量).
void main() {
var a=A(456);
print(A.att);
print(a.b);
}
class A{
static const att=123;
final b;
A(this.b);
}
as, is, if, ..., ...?
const Object i = 3; // Where i is a const Object with an int value...
const list = [i as int]; // Use a typecast.
const map = {if (i is int) i: 'int'}; // Use is and collection if.
const set = {if (list is List<int>) ...list}; // ...and a spread.
#### 如果希望由程式賦值的不變常量, 可以用final宣告
final變數**只能賦值一次就不可更改**, 會強迫宣告時就必須賦值, 除非用late修飾. const變數可以看成是final變數的簡化版.final儲存的是**固定的物件參照**, 所以參照不變, 物件內容還是可以改變.const變數儲存的參照, 物件內容都不可變.
```
final l = [1, 2, 3];
l[0] = 23; // 參照不變, list有改變
print(l); // [23, 2, 3]
// l=[4,5,6]; Can't assign to the final variable 'l'.
```
## [Null safety (include late)](https://hackmd.io/@asdf121472/rkrZ84D1Je)