Struct class abstract base

The supertype of all FFI struct types.

FFI struct types should extend this class and declare fields corresponding to the underlying native structure.

Field declarations in a Struct subclass declaration are automatically given a setter and getter implementation which accesses the native struct's field in memory.

All field declarations in a Struct subclass declaration must either have type int or double and be annotated with a NativeType representing the native type, or must be of type Pointer, Array or a subtype of Struct or Union. For example:

typedef struct {
 int a;
 float b;
 void* c;
} my_struct;
final class MyStruct extends Struct {
  @Int32()
  external int a;

  @Float()
  external double b;

  external Pointer<Void> c;
}

The field declarations of a Struct subclass must be marked external. A struct subclass points directly into a location of native memory (Pointer) or Dart memory (TypedData), and the external field's getter and setter implementations directly read and write bytes at appropriate offsets from that location. This does not allow for non-native fields to also exist.

An instance of a struct subclass cannot be created with a generative constructor. Instead, an instance can be created by StructPointer.ref, Struct.create, FFI call return values, FFI callback arguments, StructArray, and accessing Struct fields. To create an instance backed by native memory, use StructPointer.ref. To create an instance backed by Dart memory, use Struct.create.

Implemented types
Available extensions
Annotations
  • @Since('2.12')

Constructors

Struct()
Construct a reference to the nullptr.

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

create<T extends Struct>([TypedData typedData, int offset]) → T
Creates a struct view of bytes in typedData.