A function value.
TheFunction class is a supertype of allfunction types, and containsno values itself. All objects that implementFunctionhave a function type as their runtime type.
TheFunction type does not carry information about theparameter signatures or return type of a function.To express a more precise function type, use the function type syntax,which is theFunction keyword followed by a parameter list,or a type argument list and a parameter list, and which can also havean optional return type.
The function type syntax mirrors the definition of a function,with the function name replaced by the word "Function".
Example:
String numberToString(int n) => "$n";String Function(int n) fun = numberToString; // Type annotationassert(fun is String Function(int)); // Type check.List<String Function(int)> functions = [fun]; // Type argument.The typeString Function(int) is the type of a functionthat takes one positionalint argument and returns aString.
Example with generic function type:
T id<T>(T value) => value;X Function<X>(X) anotherId = id; // Parameter name may be omitted.int Function(int) intId = id<int>;A function type can be used anywhere a type is allowed,and is often used for functions taking other functions, "callbacks",as arguments.
void doSomething(String Function(int) callback) { print(callback(1));}A function type has all the members declared byObject,since function types are subtypes ofObject.
A function type also has acall method with a signaturethat has the same function type as the function type itself.Calling thecall method behaves just as calling the function.This is mainly used to conditionally call a nullable function value.
String Function(int) fun = (n) => "$n";String Function(int) fun2 = fun.call; // Valid.print(fun2.call(1)); // Prints "1".String Function(int)? maybeFun = Random().nextBool() ? fun : null;print(maybeFun?.call(1)); // Prints "1" or "null".TheFunction type has a number of special features which are not visiblein thisclass declaration.
TheFunction type itself allows any function to be assigned to it,since it is a supertype of any function type,but does not say how the function can be called.
However, a value with the static typeFunctioncan still be calledlike a function.
Function f = (int x) => "$x";print(f(1)); // Prints "1".f("not", "one", "int"); // Throws! No static warning.Such an invocation is adynamic invocation,precisely as if the function value had been statically typed asdynamic,and is precisely as unsafe as any other dynamic invocation.Checks will be performed at run-time to ensure that the argumentlist matches the function's parameters, and if not the call willfail with anError.There is no static type checking for such a call, any argument listis accepted and checked at runtime.
Like every function type has acall method with its own function type,theFunction type has a specialcall memberwhich acts as if it is a method with a function type ofFunction(which is not a method signature which can be expressed in normalDart code).
Function fun = (int x) => "$x";var fun2 = fun.call; // Inferred type of `fun2` is `Function`.print(fun2.call(1)); // Prints "1";Function? maybeFun = Random().nextBool() ? fun : null;print(maybeFun?.call(1)); // Prints "1" or "null".- Available extensions
Properties
- hashCode→int
- A hash code value that is compatible with
operator==.no setteroverride - runtimeType→Type
- A representation of the runtime type of the object.no setterinherited
- toJS→JSExportedDartFunction
Available onFunction, provided by theFunctionToJSExportedDartFunction extension
A callable JavaScript function that wraps thisFunction.no setter- toJSCaptureThis→JSExportedDartFunction
Available onFunction, provided by theFunctionToJSExportedDartFunction extension
A callable JavaScript function that wraps thisFunction and captures thethisvalue when called.no setter
Methods
- noSuchMethod(
Invocationinvocation)→ dynamic - Invoked when a nonexistent method or property is accessed.inherited
- toString(
)→String - A string representation of this object.inherited
Operators
- operator ==(
Objectother)→bool - Test whether another object is equal to this function.override