0

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.

Hugo Trentesaux
  • 1,584
  • 1
  • 16
  • 30
  • You can find more in [this](https://stackoverflow.com/questions/41369633/how-to-save-to-local-storage-using-flutter) answer. – fartem May 28 '21 at 15:48
  • @fartem it does not answer my question. I'm not in the case of using a SQL database and I wrote that "reading and writing files" official documentation does not answer my question. My question is about keeping a file open or closing it frequently. – Hugo Trentesaux May 28 '21 at 16:19

0 Answers0