-1

I have a flutter application with a folder certs/ inside the assets' folder like this: certs/ with a cert.pem file inside.

I already added the file to the pubspec.yaml:

assets:
    - assets/images/
    - assets/texts/
    - assets/certs/cert.pem

but when I run

var certFile = File("cert.pem");
var certFile = File("certs/cert.pem");
var certFile = File("assets/certs/cert.pem");
var certFile = File("../../../assets/certs/cert.pem");

None of these options work. How to do I open the file?

Thank you

AFortunato
  • 181
  • 1
  • 10

1 Answers1

0

You can't open Asset files like normal system files.

One way to access them is to use rootBundle:

Future<String> readFileFromAssets(String path) async {
  return await rootBundle.loadString(path);
}
Ali Qanbari
  • 2,923
  • 1
  • 12
  • 28
  • But this is not a text file, is a key, how should I open this type of files? – AFortunato Nov 20 '20 at 12:00
  • Use [`AssetBundle.load`](https://api.flutter.dev/flutter/services/AssetBundle/load.html) to load binary data. Read the [`AssetBundle`](https://api.flutter.dev/flutter/services/AssetBundle-class.html) documentation. – jamesdlin Nov 20 '20 at 13:02