I'm building a custom binary file storage for my flutter app and wonder what's good practice.
I can open and close the file on every change but if the write operations are frequent I guess it's better to keep it open. But if I keep the file open, how do I know when to close it to avoid getting errors like:
FileSystemException: Cannot open file, path = '/data/user/0/com.example.app/app_flutter/file' (OS Error: Too many open files, errno = 24)
Should I store a reference on the open file in my widget and access it like this:
_binaryFile.then((file) {
file.exists().then((exists) {
file.open(mode: FileMode.append).then((f) {
_storage ??= f;
if(exists) { // reads it
f.read(length).then((bytes) {
// do stuff
});
} else { // initialize it
f.writeFrom(Uint8List(length));
}
});
});
});
The read and write files page of flutter doc is rather short and does not comes with guidelines on how to manage files in a mobile app.