skip to Main Content

So i am making a windows application and i need to check the values stored in a file every few seconds, but for some reason i am unable to access the file. I am not using the regular method of accessing the file by marking its path in the pubsec.yaml file as it keeps simply storing the files value at runtime and doesnt read it multiple times even if i recall the function to read it.

Heres the error it gives me:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Unable to load asset: "Directory: 'C:Users${username_here}Documents'/data.csv".

The asset does not exist or has empty data.

Here’s the code:

Future<String> getFilePath(int which) async {
Directory appDocumentsDirectory = await getApplicationDocumentsDirectory(); String filePath = '${appDocumentsDirectory}/data.csv';
 return filePath;
 }

//Please ignore the unused argument

I have tried multiple things including manually adding the file path, changing the in the file path to / manually and i havent managed to get it to work. Any help in fixing this problem will be really appreciated. Thank you.

Edit: Heres the code where i am using it:

Future<void> loadFile() async {
    String filePath = await getFilePath(widget.which);
    final String csvDataString = await rootBundle.loadString(filePath);
    List<List<dynamic>> csvTable =
        const CsvToListConverter().convert(csvDataString);
//I am attempting to get the data and save them in variables and this is the first section 

debugPrint(‘path ${appDocumentsDirectory.path}/data.csv’); prints:


path C:Users${username}Documents/data.csv

which is the right path i want.

Edit: Managed to fix it. This comment explained the issue:https://stackoverflow.com/a/77380281/15188077

Thanks

2

Answers


  1. Directory is an object part of path_provider: ^2.1.1. Use it’s path.

    Solution

    String filePath = '${appDocumentsDirectory.path}/data.csv';
    

    Edit

    The issue appears to be passing the filePath to rootBundle.loadString(filePath), which loads files from assets path.

    Login or Signup to reply.
  2. You are mixing loading an asset with loading a file.


    In Flutter, you have two sources for files. One is the AssetBundle available globally as the variable rootBundle. This may be used to load files you bundled in your app directly by specifying them in the assets section in your pubspec.yaml.

    The other source for files is the actual filesystem of the device the app is running on. To read from that, you need to use path_provider and the dart:io class File to read the contents.

    What you are doing here is composing a path from the filesystem and then trying to read it from the rootBundle which is not going to work.


    If you want to read a file that you are bundling with the app, use the rootBundle global with the path to the file you got in your pubspec.yaml.

    If you want to read a file that is placed on the filesystem, you instead want to use a File with its readAsString method.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search