updateState method

Future<void> updateState(
  1. String type,
  2. Map<String, dynamic>? data
)

Updates the shared state for a specific type.

This method:

  1. Ensures state is synced
  2. Updates the local cache
  3. Notifies local listeners
  4. Sends the update to the native side
  5. Native side propagates to all other Flutter engines

If the state hasn't changed, no update is performed.

@param type The state type identifier @param data The state data as a JSON-serializable map, or null to clear

Implementation

Future<void> updateState(String type, Map<String, dynamic>? data) async {
  try {
    if (!_hasSyncData) {
      await _syncSharedState();
    }
    if (_cachedSharedState[type] != data) {
      _cachedSharedState[type] = data;
      debugPrint('[SharedStateManager] Updating state for $type: $data');
      _notifyListeners(type, data);
      await methodChannel.invokeMethod('updateState', {
        'type': type,
        'state': data,
      });
    }
  } on PlatformException catch (e) {
    debugPrint('Error updating shared state: ${e.message}');
  }
}