typedef 定义使用
typedef MyPrint = void Function(String val);
class PrintClass {
MyPrint print;
PrintClass(this.print);
}
main() {
PrintClass coll = PrintClass((String val) => print(val));
coll.print('hello world');
}
hello world
未采用 typedef
class PrintClass {
void Function(String val) print;
PrintClass(this.print);
}
main() {
PrintClass coll = PrintClass((String val) => print(val));
coll.print('hello world');
}
hello world
简化常用类型定义
typedef MapStringAny = Map<String, dynamic>
typedef MapAnyString = Map<dynamic, String>
typedef MapStringString = Map<String, String>
typedef MapStringDouble = Map<String, double>
typedef MapDoubleString = Map<double, String>
typedef MapDoubleDouble = Map<double, double>
typedef MapIntInt = Map<int, int>
typedef MapIntDouble = Map<int, double>
typedef ListString = List<String>
typedef ListInt = List<int>
typedef ListDouble = List<double>
typedef ListAny = List<dynamic>
main() {
ListString p = []
p.add('a')
p.add('b')
p.add('c')
MapStringAny m = {}
m['a'] = 1
m['b'] = 2
m['c'] = 3
}