Union class abstract base

The supertype of all FFI union types.

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

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

All field declarations in a Union 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 union {
 int a;
 float b;
 void* c;
} my_union;
final class MyUnion extends Union {
  @Int32()
  external int a;

  @Float()
  external double b;

  external Pointer<Void> c;
}

The field declarations of a Union subclass must be marked external. A union 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 union subclass cannot be created with a generative constructor. Instead, an instance can be created by UnionPointer.ref, Union.create, FFI call return values, FFI callback arguments, UnionArray, and accessing Union fields. To create an instance backed by native memory, use UnionPointer.ref. To create an instance backed by Dart memory, use Union.create.

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

Constructors

Union()
Construct a reference to the nullptr.

Properties

address Pointer<T>

Available on T, provided by the UnionAddress extension

The memory address of the underlying data.
no setter
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 Union>([TypedData typedData, int offset]) → T
Creates a union view of bytes in typedData.