How to get the file if I know the root directory and relative path?

This is the simplest solution I found

import 'package:path/path.dart' as path;

...

String filePath = path.join(root.path, relativePath);
filePath = path.normalize(filePath);
File f = new File(filePath);

Joining /home/name/ and ../name2 to yield /home/name2

Edit:

Thank you Günter Zöchbauer for the tip.
It seems linux boxes can handle a path like /home/name/../name2.

On a windows machine, Path.normalize needs to be used and the extra / Path.normalize preppends at the head must be removed.

Or use new Path.Context():

import 'package:path/path.dart' as Path;
import 'dart:io' show Platform,Directory;

to_abs_path(path,[base_dir = null]){
  Path.Context context;
  if(Platform.isWindows){
    context = new Path.Context(style:Path.Style.windows);
  }else{
    context = new Path.Context(style:Path.Style.posix);
  }
  base_dir ??= Path.dirname(Platform.script.toFilePath());
  path = context.join( base_dir,path);
  return context.normalize(path);
}

Tags:

File

Dart

Dart Io