SharedState<T> class
abstract
Abstract base class for creating type-safe shared state objects.
SharedState provides a reactive way to share state across multiple Flutter engines running on different displays. It extends ChangeNotifier and implements ValueListenable to integrate seamlessly with Flutter's reactive framework.
Key features:
- Type-safe state management with generic type
T - Automatic synchronization across all displays
- Integration with Flutter's ChangeNotifier pattern
- Caching for immediate access to current state
- JSON serialization/deserialization support
Example usage:
class UserProfileState extends SharedState<UserProfile> {
@override
UserProfile fromJson(Map<String, dynamic> json) {
return UserProfile.fromJson(json);
}
@override
Map<String, dynamic>? toJson(UserProfile? data) {
return data?.toJson();
}
}
// In your widget
final profileState = UserProfileState();
// Update state (will sync across all displays)
profileState.sync(UserProfile(name: 'John', age: 30));
// Listen to changes
profileState.addListener(() {
print('Profile updated: ${profileState.state}');
});
- Implemented types
-
- ValueListenable<
T?>
- ValueListenable<
- Mixed-in types
Constructors
- Creates a new SharedState instance.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- hasListeners → bool
-
Whether any listeners are currently registered.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- state → T?
-
The current state value.
no setter
- value → T?
-
Implements ValueListenable.value to return the current state.
no setteroverride
Methods
-
addListener(
VoidCallback listener) → void -
Register a closure to be called when the object changes.
inherited
-
clear(
) → void - Clears the shared state and notifies all displays.
-
dispose(
) → void -
Disposes the shared state and unregisters listeners.
override
-
fromJson(
Map< String, dynamic> json) → T -
Converts a JSON map to the state object of type
T. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
notifyListeners(
) → void -
Call all the registered listeners.
inherited
-
removeListener(
VoidCallback listener) → void -
Remove a previously registered closure from the list of closures that are
notified when the object changes.
inherited
-
sync(
T? state) → void - Updates the shared state and notifies all displays.
-
toJson(
T? data) → Map< String, dynamic> ? - Converts the state object to a JSON map.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited