creation_of_struct_or_union
'Struct' 和 'Union' 的子類由原生記憶體支援,不能透過生成式建構函式例項化。
描述
#當使用生成式建構函式例項化 Struct 或 Union 的子類時,分析器會生成此診斷。
有關 FFI 的更多資訊,請參閱 使用 dart:ffi 進行 C 語言互操作。
示例
#以下程式碼會產生此診斷,因為類 C 正在使用生成式建構函式進行例項化
dart
import 'dart:ffi';
final class C extends Struct {
@Int32()
external int a;
}
void f() {
C();
}常見修復方法
#如果您需要分配類描述的結構體,請使用 ffi 包進行操作
dart
import 'dart:ffi';
import 'package:ffi/ffi.dart';
final class C extends Struct {
@Int32()
external int a;
}
void f() {
final pointer = calloc.allocate<C>(4);
final c = pointer.ref;
print(c);
calloc.free(pointer);
}