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 {
  final storageKey = key ?? defaultKey;
  final prefKey = _getStorageKey(storageKey);

  try {
    final prefs = await _prefs;
    final jsonString = prefs.getString(prefKey);
    if (jsonString == null) {
      debugPrint('[Checkpoint] Snapshot not found in SharedPreferences: $storageKey');
      return null;
    }

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