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 {
  await _ensureDirectory();

  final storageKey = key ?? defaultKey;
  final file = _getFile(storageKey);

  try {
    final json = SnapshotSerializer.instance.toJson(snapshot);
    final jsonString = jsonEncode(json);
    await file.writeAsString(jsonString);
    debugPrint('[Checkpoint] Saved snapshot to ${file.path}');
  } catch (e) {
    throw StateError('Failed to save snapshot to ${file.path}: $e');
  }
}