must_ be_ a_ native_ function_ type
The type '{0}' given to '{1}' must be a valid 'dart:ffi' native function type.
Description
#
The analyzer produces this diagnostic when an invocation of either
Pointer.fromFunction, DynamicLibrary.lookupFunction,
or a NativeCallable constructor, has a type
argument(whether explicit or inferred) that isn't a native function type.
For more information about FFI, see C interop using dart:ffi.
Example
#
The following code produces this diagnostic because the type T can be
any subclass of Function but the type argument for fromFunction
is
required to be a native function type:
import 'dart:ffi';
int f(int i) => i * 2;
class C<T extends Function> {
void g() {
Pointer.fromFunction<T>(f, 0);
}
}
Common fixes
#Use a native function type as the type argument to the invocation:
import 'dart:ffi';
int f(int i) => i * 2;
class C<T extends Function> {
void g() {
Pointer.fromFunction<Int32 Function(Int32)>(f, 0);
}
}
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Page last updated on 2025-9-1. View source or report an issue.