I found that problem to find relative path of a file inside of my test script file, so I improved the answer of @TastyCatFood to work in that context too. The following script can find the relative of a file every where:
import 'dart:io';
import 'package:path/path.dart' as path;
/// Find the path to the file given a name
/// [fileName] : file name
/// [baseDir] : optional, base directory to the file, if not informed, get current script path.
String retrieveFilePath(String fileName, [String baseDir]){
var context;
// get platform context
if(Platform.isWindows) {
context = path.Context(style:path.Style.windows);
} else {
context = path.Context(style:path.Style.posix);
}
// case baseDir not informed, get current script dir
baseDir ??= path.dirname(Platform.script.path);
// join dirPath with fileName
var filePath = context.join(baseDir, fileName);
// convert Uri to String to make the string treatment more easy
filePath = context.fromUri(context.normalize(filePath));
// remove possibles extra paths generated by testing routines
filePath = path.fromUri(filePath).split('file:').last;
return filePath;
}
The following example read the file data.txt in the same folder of main.dart
file:
import 'package:scidart/io/io.dart';
main(List<String> arguments) async {
File f = new File('data.txt');
}