一,关于的typedef官方定义
- 官方说明:
In Dart, functions are objects, just like strings and numbers are objects. A typedef, or function-type alias, gives a function type a name that you can use when declaring fields and return types. A typedef retains type information when a function type is assigned to a variable.
- 大概含义
typedef 给某一种特定的函数类型起了一个名字,可以认为是一个类型的别名,可以类比class和对象这样理解:自己定义了一种数据类型,不过这种数据类型是函数类型,一个一个的具体实现的函数就相当于按照这种类型实例化的对象会有类型检查
二,Flutter中ValueChanged<T>的定义
typedef ValueChanged<T>= void Function(T value);
三,示例
typedef Fly = void Function(int value);
void main(){ Bird bird = Bird((int a){print(a);});//如果实参函数的类型不是该类型的话 编译不通过 bird.fly(3); } class Bird{ Fly fly; Bird(this.fly); }