saveData method
Saves data to storage.
key is the unique identifier for the data.
data is the Map of data to be saved.
If compress is true, the data will be compressed before saving.
If encrypt is true, the data will be encrypted before saving.
Implementation
Future<void> saveData(String key, Map<String, dynamic> data,
{bool compress = false, bool encrypt = false}) async {
try {
debugPrint('Saving data for key: $key');
final String jsonData = json.encode(data);
String processedData = jsonData;
if (compress) {
processedData = _compressString(processedData);
debugPrint('Data compressed for key: $key');
}
if (encrypt) {
if (_encrypter == null) {
throw Exception(
'Encryption key not set. Call initialize with an encryption key before using encryption.');
}
processedData = _encryptString(processedData);
debugPrint('Data encrypted for key: $key');
}
final String hash = _calculateHash(jsonData);
await _box.put(key, {
'data': processedData,
'hash': hash,
'version': 1, // Increment this when you change the data structure
'compressed': compress,
'encrypted': encrypt,
});
debugPrint('Data saved successfully for key: $key');
} catch (e) {
debugPrint('Failed to save data for key $key: $e');
rethrow;
}
}