FloatyStateChannel<S> class final

A typed, auto-syncing state channel between the main app and overlay.

Instead of sending raw Map<String, dynamic> payloads, define a state class with toJson/fromJson and let the channel keep both sides in sync.

Main app side:

final channel = FloatyStateChannel<MyState>(
  toJson: (s) => s.toJson(),
  fromJson: MyState.fromJson,
  initialState: MyState(),
);

// Full replace + sync.
await channel.setState(MyState(count: 1));

// Partial update (shallow merge).
await channel.updateState({'count': 2});

// Listen for changes from the overlay.
channel.onStateChanged.listen((state) => print(state));

Overlay side:

final channel = FloatyStateChannel<MyState>.overlay(
  toJson: (s) => s.toJson(),
  fromJson: MyState.fromJson,
  initialState: MyState(),
);

The state is serialized as JSON and transmitted through the shared data channel. Both sides must use the same state type and serialization.

Note: updateState performs a shallow merge of top-level keys. Nested objects are replaced, not recursively merged.

Constructors

FloatyStateChannel({required Map<String, dynamic> toJson(S state), required S fromJson(Map<String, dynamic> json), required S initialState})
Creates a state channel for the main app side.
FloatyStateChannel.overlay({required Map<String, dynamic> toJson(S state), required S fromJson(Map<String, dynamic> json), required S initialState})
Creates a state channel for the overlay side.
factory

Properties

hashCode int
The hash code for this object.
no setterinherited
onStateChanged Stream<S>
Stream of state changes from the other side.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
state → S
The current state (synchronous read).
no setter

Methods

dispose() Future<void>
Releases resources.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
setState(S newState) Future<void>
Replaces the entire state and syncs to the other side.
toString() String
A string representation of this object.
inherited
updateState(Map<String, dynamic> partial) Future<void>
Performs a shallow merge of partial into the current state's JSON representation, rebuilds the state, and syncs to the other side.

Operators

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