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);
}
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。