updateState method
Updates the shared state for a specific type.
This method:
- Ensures state is synced
- Updates the local cache
- Notifies local listeners
- Sends the update to the native side
- 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}');
}
}