BinaryContract<T> class abstract

The BinaryContract allows developers to define a strongly typed binary representation of a Dart class. If your Dart class contract changes, so will the binary contract, ensuring you never forget to change how the data is being read or stored.

BinaryContract works best with classes that are immutable, because it will implement your class by overwriting the fields as getters. If your fields are not final Dart will complain that the setter was not implement. There are ways to work around this but it is not recommended.

An example of how you can implement a contract for your own classes:

class MyClass {
  const MyClass({
    required this.myProperty,
  });

  final int myProperty;
}

/// Defining the contract for the [MyClass] class. It should also implement the
/// class so the contract can access the fields.
class _MyClassContract extends BinaryContract<MyClass> implements MyClass {
  /// Pass a base line instance to the super constructor. This is used to build
  /// up the, contract, the values defined in the base line do not matter.
  const _MyClassContract()
      : super(const MyClass(myProperty: 1337));

  /// This method allows the [BinaryContract] to know the order of how the binary
  /// data should be read and stored.
  ///
  /// [contract] is always the [BinaryContract] instance.
  @override
  MyClass order(MyClass contract) {
    return MyClass(
      myProperty: contract.myProperty,
    );
  }

  /// The properties of the [MyClass] need to be implemented as getters.
  @override
  int get myProperty => type(uint16, (o) => o.myProperty);
}

// Create a constant instance of the contract.
const myClassContract= _MyClassContract();

void main() {
  // Create a new instance of MyClass.
  final myClass = MyClass(myProperty: 1337);

  // Store the instance using the contract.
  final writer = Payload.write()..set(myClassContract, myClass);
  final bytes = binarize(writer);

  // Read it back from binary.
  final reader = Payload.read(bytes);
  final myClassAgain = reader.get(myClassContract);

  print(myClassAgain.myProperty); // Returns 1337
}

The BinaryContract extends from PayloadType and can be used within other payloads, including itself.

Inheritance

Constructors

BinaryContract(T _base)
The BinaryContract allows developers to define a strongly typed binary representation of a Dart class. If your Dart class contract changes, so will the binary contract, ensuring you never forget to change how the data is being read or stored.
const

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

get(ByteData data, int offset) → T
Retrieve the value from the data at the given offset.
override
length(T value) int
Returns the byte length of the PayloadType with the given value.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
order(T contract) → T
Define what the order is in which the binary data should be read or stored.
set(T value, ByteData data, int offset) → void
Set the value in the data at the given offset.
override
toString() String
A string representation of this object.
inherited
type<V>(PayloadType<V> type, V resolve(T o)) → V
Define a property's type and how the value should be resolved on the object type (T).

Operators

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