類型別名
類型別名(通常稱為 typedef,因為它使用關鍵字 typedef 宣告)是引用型別的一種簡潔方式。以下是宣告和使用名為 IntList 的類型別名的示例
dart
typedef IntList = List<int>;
IntList il = [1, 2, 3];類型別名可以有型別引數
dart
typedef ListMapper<X> = Map<X, List<X>>;
Map<String, List<String>> m1 = {}; // Verbose.
ListMapper<String> m2 = {}; // Same thing but shorter and clearer.在大多數情況下,我們建議為函式使用行內函數型別而不是 typedefs。然而,函式 typedefs 仍然可能有用
dart
typedef Compare<T> = int Function(T a, T b);
int sort(int a, int b) => a - b;
void main() {
assert(sort is Compare<int>); // True!
}