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:
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:
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);
}
Unless stated otherwise, the documentation on this site reflects Dart 3.7.3. Page last updated on 2025-05-08. View source or report an issue.