save method

  1. @override
Future<void> save(
  1. Snapshot snapshot, [
  2. String? key
])
override

Saves a snapshot to storage.

Parameters:

  • snapshot: The snapshot to save
  • key: Optional key/identifier for the snapshot. If not provided, a default key will be used.

Returns: A Future that completes when the snapshot is saved.

Throws:

  • Storage-specific exceptions (e.g., IOException for file storage)

Implementation

@override
Future<void> save(Snapshot snapshot, [String? key]) async {
  final storageKey = key ?? defaultKey;
  final prefKey = _getStorageKey(storageKey);

  try {
    final json = SnapshotSerializer.instance.toJson(snapshot);
    final jsonString = jsonEncode(json);
    final prefs = await _prefs;
    await prefs.setString(prefKey, jsonString);
    debugPrint('[Checkpoint] Saved snapshot to SharedPreferences with key: $storageKey');
  } catch (e) {
    throw StateError('Failed to save snapshot to SharedPreferences: $e');
  }
}