9

Here is the setup in side of my pubspec.yaml
...

path_provider: ^0.4.1

assets:
    - assets/
    - assets/a.txt

...

I try to find way to access to the "assets" directory to read content of the text file named "a.txt" where it should read in relative way as "assets/a.txt". Here is my code in my main.dart:

Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
String fPath = '${appDocDir.parent.path}/assets/a.txt';
new File(fPath).readAsString().then((String contents) {
    print("FILE:..... "+contents);
}); 
            

And here I got this error:

[VERBOSE-2:shell.cc(184)] Dart Error: Unhandled exception:

FileSystemException: Cannot open file, path = '/var/mobile/Containers/Data/Application/EE2A0E91-6543-4CE3-A82B-3B69BB892BFC/assets/a.txt' (OS Error: No such file or directory, errno = 2)

Seem like the ... EE2A0E91-6543-4CE3-A82B-3B69BB892BFC/ is not the root directory of the app to concat with my file assets/a.txt.

How can I find the exact absolute path of my a.txt file in order to read it using File() class?

Melvin
  • 925
  • 1
  • 10
  • 22
Sotheayuth
  • 126
  • 1
  • 6
  • Does this answer your question? [How do I get the Asset's file path in flutter?](https://stackoverflow.com/questions/52353764/how-do-i-get-the-assets-file-path-in-flutter) – Jannie Theunissen Mar 04 '20 at 12:02

1 Answers1

-4

You need to use AssetBundle to read assets.

For example https://docs.flutter.io/flutter/services/AssetBundle/load.html or https://docs.flutter.io/flutter/services/AssetBundle/loadString.html

See also https://flutter.io/docs/development/ui/assets-and-images#loading-text-assets

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567