Home > database >  How to convert relative path to absolute path in dart?
How to convert relative path to absolute path in dart?

Time:06-26

In nodejs I simply use path

import path from "path";
path.resolve(__dirname, '../', './test.txt');

Is there any way to use in dart ?

CodePudding user response:

You probably want to use File.absolute.path/Directory.absolute.path.

CodePudding user response:

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

void main(List<String> args) {
  final relative = 'example/example.dart';
  final absolute = path.normalize(path.absolute(relative));
  print(absolute);

  final dir = path.dirname(absolute);
  print(dir);
  final absolute2 =
      path.normalize(path.absolute(path.join(dir, '../', './test.txt')));
  print(absolute2);
}

Output (in my case):

E:\prj\dart_test\example\example.dart
E:\prj\dart_test\example
E:\prj\dart_test\test.txt

P.S.
I prefer to use normalize but this is matter of taste.

  • Related