SharedState<T> constructor

SharedState<T>()

Creates a new SharedState instance.

The state type identifier is automatically derived from the generic type T using T.toString().

Upon creation:

  1. Registers a listener with SharedStateManager
  2. Loads cached state if available
  3. Syncs state from native side asynchronously

Implementation

SharedState() : _type = T.toString() {
  // Automatically register listener with SharedStateManager
  SharedStateManager.instance.addStateChangeListener(_type, _onStateChange);

  // Initialize with cached state if available
  final cachedState = SharedStateManager.instance.getCachedState(_type);
  if (cachedState != null) {
    _state = fromJson(cachedState);
    debugPrint('[SharedState] Initial cached state for $_type: $_state');
  } else {
    // Sync state asynchronously
    _syncState();
  }
}