create method
- T data
Creates a file in the data directory with the given data.
The data
parameter represents the data to be stored in the file.
The idGetterFunction is a function that retrieves the ID for the data.
The toJsonFunction is a function that converts the data to JSON format.
The encriptionKey is an optional encryption key used to encrypt the JSON data.
Throws an exception if there is an error writing the file.
Implementation
Future<K> create(T data) async {
final id = idGetterFunction(data);
final file = File('${directory.path}/$id.json');
final json = jsonEncode(toJsonFunction(data));
final encryptedJson = encriptionKey.isNotEmpty ? json.applyXorEncrypt(encriptionKey) : json; // Encrypt the JSON if key is not blank
await file.writeAsString(encryptedJson);
return id;
}