In our flutter app we are saving data to a file like capturing major events related to user. This capture happens from multiple places. As it happens from multiple places data is getting mixed because of that when reading data and decoding to base64 i am getting format exception.
I am taking the model class converting to map, then doing a json encode and then base64 encode and converting it to bytes and writing to file. When reading the file i am doing all these steps in reverse.
As the write to file happens from multiple places i took a separate variable and adding data to it. please check the code below and tell if i am doing anything wrong.
I tried writeAsBytesSync also it's not at all working.
static List dataToBeWritten = [];
static Future<bool> savedata(User user) async {
dataToBeWritten.add(user);
if (isWriting) {
return true;
} else {
isWriting = true;
File auditFile = await createOrOpenAuditFile();
dataToBeWritten.forEach((element) async {
var encoded = json.encode(element.toMap());
var bas64Encoded = base64.encode(encoded.codeUnits);
bas64Encoded = bas64Encoded + '\n';
await auditFile.writeAsBytes(bas64Encoded.codeUnits, mode: FileMode.append, flush: true);
dataToBeWritten.remove(element);
});
isWriting = false;
return true;
}
}