restoreBackupJson method
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 typeK.jsonToValue: Function to convert each JSON value to a value of typeT.
This method will:
- Decode the JSON string into a map.
- Convert each key and value using the provided functions.
- 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);
}