load method

  1. @override
Future<Snapshot?> load([
  1. String? key
])
override

Loads a snapshot from storage.

Parameters:

  • key: Optional key/identifier for the snapshot. If not provided, the default/latest snapshot will be loaded.

Returns: A Future that completes with the loaded Snapshot, or null if no snapshot exists for the given key.

Throws:

  • FormatException if the stored data is invalid
  • Storage-specific exceptions (e.g., IOException for file storage)

Implementation

@override
Future<Snapshot?> load([String? key]) async {
  await _ensureDirectory();

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

  if (!await file.exists()) {
    debugPrint('[Checkpoint] Snapshot file not found: ${file.path}');
    return null;
  }

  try {
    final jsonString = await file.readAsString();
    final json = jsonDecode(jsonString) as Map<String, dynamic>;
    final snapshot = SnapshotSerializer.instance.fromJson(json);
    debugPrint('[Checkpoint] Loaded snapshot from ${file.path}');
    return snapshot;
  } catch (e) {
    throw FormatException(
      'Failed to load snapshot from ${file.path}: $e',
      file.path,
    );
  }
}