When you want to reference a file in the lib
directory you use a package-relative path.
packages/my_package/lib/rules/rules.json
When the file from where you want to reference to another file is not in one of the top-level directories like web
, example
, bin
, test
, ... you have to navigate to the top before going down through the packages directory.
This way works within every file to every target file inside the lib
directory of the current app package or any package you have declared as dependency in pubspec.yaml
.
../packages/my_package/lib/rules/rules.json
pub build
gives hints when you are not navigating up enough.
Example
the file containing main is in playground/bin/script/script_path/main.dart
the test.json
file is in playground/lib/some_json
import 'dart:io' as io;
main() {
Uri filePath = io.Platform.script.resolve(
'../../../packages/playground/some_json/test.json');
var file = new io.File.fromUri(filePath);
var content = file.readAsString().then((c) {
print(c);
});
}
On the server you don't need to navigate up (at least currently) because each directory has a symlink to the packages
directory, but there are plans to get rid of symlinks and dart2dart
probably works like dart2js
and may make it necessary to navigate up first like in browser apps.