4

Suppose the following files:

lib/src/program.dart

lib/src/file.txt

I want to read file.txt contents from program.dart using a library that contains readingFunction(path):

//program.dart
String fileContents = library.readingFunction("./file.txt");

This throws an error, because the current path is the one in which execution was launched (or the package path if executing with pub run).

How could I achieve the reading with relative path instead of being forced to use "./lib/src/file.txt" path?

Community
  • 1
  • 1
Nico Rodsevich
  • 2,393
  • 2
  • 22
  • 32

1 Answers1

2

Use the Resource package.

It allows you to read files that are embedded in packages. Files are specified with a special URL that looks like package:package_name/path/to/file.txt (same as used in import statements).

Argenti Apparatus
  • 3,857
  • 2
  • 25
  • 35