What is the use of typedef in dart?
In Dart, typedef
is used to define function type aliases, which makes it easier to declare variables, parameters, or return types of functions with specific signatures.
Syntax:
typedef FunctionName = ReturnType Function(ParameterType1 param1, ParameterType2 param2);
Example:
typedef MathOperation = int Function( int a, int b );
int add( int x, int y ){
return x + y;
}
int multiply( int x, int y ){
return x * y;
}
void main( ){
MathOperation operation;
operation = add;
print( operation( 3, 4 ) );
operation = multiply;
print( operation( 3, 4 ) );
}
Output:
