SharedStateManager class
Singleton manager for coordinating shared state across multiple Flutter engines.
SharedStateManager acts as a bridge between Flutter and the native Android SharedStateManager, providing a unified API for state management across all displays in a multi-display application.
Features:
- Singleton pattern for global access
- Method channel communication with native code
- Local state caching for improved performance
- Automatic state synchronization across all engines
- Type-safe listener registration
This manager is automatically initialized when accessed and handles:
- Bidirectional state synchronization
- Listener management and notification
- State caching to reduce native calls
- Initial state loading on startup
Example usage:
// Get the singleton instance
final manager = SharedStateManager.instance;
// Update state
await manager.updateState('cartTotal', {'amount': 99.99, 'currency': 'USD'});
// Get state
final cartData = await manager.getState('cartTotal');
// Listen to changes
manager.addStateChangeListener('cartTotal', (data) {
print('Cart updated: $data');
});
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- methodChannel ↔ MethodChannel
-
Method channel for communication with native Android code.
latefinal
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
addStateChangeListener(
String type, OnSharedStateChangeListener listener) → void - Registers a listener for state changes of a specific type.
-
clearState(
String type) → Future< void> - Clears the shared state for a specific type.
-
getCachedState(
String type) → Map< String, dynamic> ? - Gets the cached state for a specific type without triggering a sync.
-
getState(
String type) → Future< Map< String, dynamic> ?> - Retrieves the shared state for a specific type.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
removeStateChangeListener(
String type, OnSharedStateChangeListener listener) → void - Unregisters a previously registered listener.
-
toString(
) → String -
A string representation of this object.
inherited
-
updateState(
String type, Map< String, dynamic> ? data) → Future<void> - Updates the shared state for a specific type.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- instance → SharedStateManager
-
Returns the singleton instance of SharedStateManager.
no setter