creation_of_struct_or_union
Subclasses of 'Struct' and 'Union' are backed by native memory, and can't be instantiated by a generative constructor.
Description
#The analyzer produces this diagnostic when a subclass of either Struct
or Union
is instantiated using a generative constructor.
For more information about FFI, see C interop using dart:ffi.
Example
#The following code produces this diagnostic because the class C
is being
instantiated using a generative constructor:
dart
import 'dart:ffi';
final class C extends Struct {
@Int32()
external int a;
}
void f() {
C();
}
Common fixes
#If you need to allocate the structure described by the class, then use the
ffi
package to do so:
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);
}
除非另有说明,文档之所提及适用于 Dart 3.7.3 版本,本页面最后更新时间: 2025-05-08。 查看文档源码 或者 报告页面问题。