I am writing a library in Dart and I have static files under the library folder. I want to be able to read those files, but I'm not sure how to retrieve the path to it... there is not __FILE__
or $0
like in some other languages.
Update: It seems that I was not clear enough. Let this help you understand me:
test.dart
import 'foo.dart';
void main() {
print(Foo.getMyPath());
}
foo.dart
library asd;
class Foo {
static Path getMyPath() => new Path('resources/');
}
It gives me the wrong folder location. It gives me the path to test.dart
+ resources/
, but I want the path to foo.dart
+ resources/
.
The dart mirrors API (still experimental, and not available on all platforms such as dart2js yet) exposes a url getter on the LibraryMirror. This should give you what you want.
I'm not aware of any other way to get this information on a library.
Generally the usual method of accessing resources which are located at a static position with your library is by use using a relative path.
See addition API information on Path and on File
As an aside.. If you absolutely wanted to get the same type of output as
__FILE__
you can do something like the following:As mentioned, you can use mirrors. Here's an example using what you wanted to achieve:
test.dart
foo.dart
It should output something like:
There will probably be a better way to do this in a future release. I will update the answer when this is the case.
Update: You could also define a private method in the library: