appendDataToFile method
Appends data to an existing file.
- When
pathis provided it takes precedence overfileName. - Otherwise the file is looked up inside filesPath using
fileName. dataTypedrives howdatais appended: strings are appended as text, base64 strings are decoded first, bytes are appended as-is.
Does nothing when data is null/empty or when the target file does
not exist.
Implementation
Future<void> appendDataToFile(
Object? data,
DataType dataType, {
String? fileName,
String? path,
}) async {
if (data == null) return;
if (data is String && data.isEmpty) return;
if (data is List && data.isEmpty) return;
final String? validated = validatePath(path);
await _locked(_lockKey(fileName, validated), () async {
// Never through the shared 2.0.3 folder: only this application's file.
// Only its first bytes are read to know whether it is encrypted.
final Uint8List? head = await _store.readHead(
fileMagic.length,
fileName: fileName,
path: validated,
);
if (head == null) return;
if (!hasFileMagic(head)) {
await _store.append(
_bytesOf(data, dataType),
fileName: fileName,
path: validated,
);
return;
}
final Uint8List clear = await _open(
(await _store.read(
fileName: fileName,
path: validated,
fallback: false,
))!,
);
await _store.write(
await _seal(<int>[...clear, ..._bytesOf(data, dataType)], true),
fileName: validated == null ? fileName : null,
path: validated,
recursive: true,
);
});
}