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);
}