address_receiver
`.address` 的接收者必須是具體的 `TypedData`、具體的 `TypedData` 的 `[]` 訪問結果、`Array`、`Array` 的 `[]` 訪問結果、Struct 欄位或 Union 欄位。
描述
#當 `.address` getter 用於靜態型別不是允許的 FFI 型別之一的接收者時,分析器會生成此診斷。`.address` getter 用於獲取 FFI 資料結構底層記憶體的 `Pointer`。
`.address` 的接收者必須是以下型別之一
- 一個具體的 `TypedData` 例項(例如,`Uint8List`)。
- 透過 `[]` 訪問的具體 `TypedData` 例項的元素。
- 一個 `Array
` 例項(來自 `dart:ffi`)。 - 透過 `[]` 訪問的 `Array
` 例項的元素。 - 一個 `Struct` 或 `Union` 子類的欄位,如果該欄位的型別是 `Array
`、巢狀 `Struct` 或巢狀 `Union`。 - 一個 `Struct` 或 `Union` 例項。
示例
#以下程式碼對各種不正確的接收者生成此診斷
dart
import 'dart:ffi';
final class MyStruct extends Struct {
@Uint8()
external int x;
@Uint8()
external int y;
}
@Native<Void Function(Pointer)>(isLeaf: true)
external void nativeLeafCall(Pointer ptr);
void main() {
final struct = Struct.create<MyStruct>();
final y = struct.y;
// Incorrect: The receiver is not a struct field, but some integer.
nativeLeafCall(y.address);
}常見修復
#確保 `.address` getter 的接收者是允許的型別之一。`.address` getter 用於獲取 `TypedData`、`Array`、`Struct` 或 `Union` 例項及其某些欄位/元素的記憶體 `Pointer`。
dart
import 'dart:ffi';
@Native<Void Function(Pointer)>(isLeaf: true)
external void nativeLeafCall(Pointer ptr);
final class MyStruct extends Struct {
@Uint8()
external int x;
@Uint8()
external int y;
}
void main() {
final struct = Struct.create<MyStruct>();
// Correct: The receiver is a struct field.
nativeLeafCall(struct.y.address);
}