leaf_call_must_not_return_handle
FFI 叶调用不能返回 'Handle' 类型。
描述
#当 Pointer.asFunction 或 DynamicLibrary.lookupFunction 调用中的 isLeaf 参数的值为 true 且返回的函数返回类型为 Handle 时,分析器会生成此诊断。
当 Native 注解中的 isLeaf 参数值为 true 且该注解上的类型参数是返回类型为 Handle 的函数类型时,分析器也会生成此诊断。
在所有这些情况下,叶调用仅支持类型为 bool、int、float、double,以及作为返回类型的 void。
有关 FFI 的更多信息,请参阅使用 dart:ffi 进行 C 互操作。
示例
#以下代码会生成此诊断,因为函数 p 返回 Handle,但 isLeaf 参数为 true
dart
import 'dart:ffi';
void f(Pointer<NativeFunction<Handle Function()>> p) {
p.asFunction<Object Function()>(isLeaf: true);
}常见修复方法
#如果函数返回句柄,则移除 isLeaf 参数
dart
import 'dart:ffi';
void f(Pointer<NativeFunction<Handle Function()>> p) {
p.asFunction<Object Function()>();
}如果函数返回受支持的类型之一,则更正类型信息
dart
import 'dart:ffi';
void f(Pointer<NativeFunction<Int32 Function()>> p) {
p.asFunction<int Function()>(isLeaf: true);
}