StateContributor<T> class

Contract for state contributors that can export and import their state.

A state contributor represents a piece of application state that can be captured in a snapshot and restored later. Each contributor must provide:

  • An exporter function that captures the current state
  • An importer function that restores state from captured data

Key Requirements:

  1. Purity (Exporter): The exporter function must be pure - no side effects, deterministic output based on current state.

  2. Idempotency (Importer): The importer function must be idempotent - calling it multiple times with the same data produces the same result.

  3. JSON-Serializable Data: Both exporter output and importer input must be JSON-serializable (Map, List, primitives). No BuildContext, Streams, Controllers, Futures, platform handles, or closures.

Example Usage:

class UserState {
  String name = '';
  int age = 0;
  String email = '';

  Map<String, dynamic> toJson() => {
    'name': name,
    'age': age,
    'email': email,
  };

  void restore(Map<String, dynamic> json) {
    name = json['name'] as String;
    age = json['age'] as int;
    email = json['email'] as String;
  }
}

// Register the state contributor
StateSnapshot.register<UserState>(
  key: 'user_state',
  exporter: () => userState.toJson(),
  importer: (json) => userState.restore(json),
);

Type Parameter:

  • T: The type of the state object being contributed (used for type safety in the registration API, but not enforced at runtime).

Constructors

StateContributor({required StateExporter exporter, required StateImporter importer})
Creates a state contributor with the given exporter and importer.
const

Properties

exporter StateExporter
Function that exports state as a JSON-serializable map.
final
hashCode int
The hash code for this object.
no setterinherited
importer StateImporter
Function that imports state from a JSON-serializable map.
final
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