argument_must_be_a_constant
引數“{0}”必須是一個常量。
描述
#當呼叫 Pointer.asFunction 或 DynamicLibrary.lookupFunction 時,如果其 isLeaf 引數的值不是常量表達式,分析器會產生此診斷資訊。
當呼叫 Pointer.fromFunction 或 NativeCallable.isolateLocal 時,如果其 exceptionalReturn 引數的值不是常量表達式,分析器也會產生此診斷資訊。
有關 FFI 的更多資訊,請參閱 使用 dart:ffi 進行 C 互操作。
示例
#以下程式碼會產生此診斷資訊,因為 isLeaf 引數的值是一個形參,因此不是常量
dart
import 'dart:ffi';
int Function(int) fromPointer(
Pointer<NativeFunction<Int8 Function(Int8)>> p, bool isLeaf) {
return p.asFunction(isLeaf: isLeaf);
}常見修復方法
#如果存在可用的合適常量,請將引數替換為該常量
dart
import 'dart:ffi';
const isLeaf = false;
int Function(int) fromPointer(Pointer<NativeFunction<Int8 Function(Int8)>> p) {
return p.asFunction(isLeaf: isLeaf);
}如果不存在合適的常量,請將引數替換為布林值字面量
dart
import 'dart:ffi';
int Function(int) fromPointer(Pointer<NativeFunction<Int8 Function(Int8)>> p) {
return p.asFunction(isLeaf: true);
}