1

I am working on an app which gathers text content from server and as the next step I am trying to save them into separate files to be saved with in apps storage. Storage is a folder called 'storage'. Inside storage, there is a 'fileList.txt' file which contains the list of files.

In pubspec.yaml I declared this folder in the assets as follows:

 assets:
- storage/

And I can read it using the following function:

Future<String> readFileContent() async {
    String response;
    response =
        await rootBundle.loadString('storage/fileList.txt');
    return response;
  }

To save the file I made use of online examples which use 'path_provider' package. However when I try to save one of the downloaded files for instance 'file4.dat' using the following functions, I encounter the following error. How can I resolve that problem?

  saveFileLocally() async {
    await _writeFile();
  }
  
  Future<Null> _writeFile() async {
    await (await _getLocalFile()).writeAsString(vars.fileContents);
  }

  Future<File> _getLocalFile() async {
    // get the path to the document directory.
    String dir = (await PathProvider.getApplicationSupportDirectory()).path;
    print('DIR :::: ' + dir);
    return new File(
        '$dir/storage/files/file' + vars.newFileID.toString() + '.dat');
  }

The error is:

Unhandled Exception: FileSystemException: Cannot open file, 
path = '/data/user/0/com.example.ferenova_flutter_app/files/storage/file4.dat'
(OS Error: No such file or directory, errno = 2)

Thanks guys and take care !!! :)

dramaticlook
  • 653
  • 1
  • 12
  • 39

2 Answers2

6

While checking out path_provider examples, I ran into this important detail:

Directory directory = Platform.isAndroid
        ? await getExternalStorageDirectory() //FOR ANDROID
        : await getApplicationSupportDirectory(); //FOR iOS

This solved my problem. I am no longer using default rootBundle asset to process any external files.

dramaticlook
  • 653
  • 1
  • 12
  • 39
0

I think you should check your paths.

storage/fileList.txt is not the same as $dir/storage/files/file' + vars.loadedFileID.toString() + '.dat'

you should try:

response =
        await rootBundle.loadString('$dir/storage/files/file' + vars.loadedFileID.toString() + '.dat'');

Or something similar.
GL

miguelik
  • 475
  • 4
  • 11
  • Hi thanks for the answer:) I think there has been a misunderstanding. Those two files are different files. One is ".txt" and the other is a ".dat" file. I need to create the ".dat" file and creation is an issue. rootBundle is used to read and it is working without any problem. – dramaticlook Oct 23 '20 at 13:00
  • Ok then... Are you sure that the path you are using actually exists in your device? From this link: https://api.dart.dev/stable/2.10.2/dart-io/File/File.html My guess is that you harcoded a Path you want your files to be in but this path does not exist. try $dir+ vars.newFileID.toString() + '.dat' Maybe this exactly does not work but thats my idea. GL – miguelik Oct 23 '20 at 16:10
  • I have no idea :D I am using a library that is referred in many tutorials and I am following the suggested technique. Even the code I used is a copy from the tutorials. – dramaticlook Oct 23 '20 at 16:13
  • can you paste that library?/ post the full code for testing – miguelik Oct 24 '20 at 07:43
  • As I mentioned in the question, I used “path_provider” library. – dramaticlook Oct 26 '20 at 04:56