Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read files relatives to the .dart file? [duplicate]

Tags:

dart

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?

like image 448
Nico Rodsevich Avatar asked Oct 31 '25 16:10

Nico Rodsevich


1 Answers

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).

like image 105
Argenti Apparatus Avatar answered Nov 02 '25 19:11

Argenti Apparatus