must_ be_ a_ native_ function_ type
Details about the 'must_be_a_native_function_type' diagnostic produced by the Dart analyzer.
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);
}
}
除非另有说明,文档之所提及适用于 Dart 3.12.2 版本报告页面问题.