restoreBackupJson method

Future<void> restoreBackupJson(
  1. String json, {
  2. required K stringToKey(
    1. String key
    ),
  3. required T jsonToValue(
    1. dynamic json
    ),
})

Restores the box contents from a JSON string backup.

  • json: The JSON string representing the backup, as produced by generateBackupJson.
  • stringToKey: Function to convert each string key in the JSON to a key of type K.
  • jsonToValue: Function to convert each JSON value to a value of type T.

This method will:

  1. Decode the JSON string into a map.
  2. Convert each key and value using the provided functions.
  3. Clear the box and insert all restored entries.

Returns a Future that completes when the restore operation is finished.

Throws BoxNotInitializedException if the box is not initialized. Throws FormatException if the JSON is invalid.

Implementation

Future<void> restoreBackupJson(
  String json, {
  required K Function(String key) stringToKey,
  required T Function(dynamic json) jsonToValue,
}) async {
  await ensureInitialized();
  final Map<String, dynamic> decoded = jsonDecode(json);
  final Map<K, T> restored = {
    for (final entry in decoded.entries)
      stringToKey(entry.key): jsonToValue(entry.value)
  };
  await clear();
  await putAll(restored);
}