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

Issue

In Dart, if I know the root directory and the relative path of a file, how to create a file instance for it?

Directory root = new Directory("/root");
String relativePath = "logs/users.log";

How to create a file instance for the users.log?

In java, it’s very simple:

new File(root, relativePath);

But in Dart, I can’t find a simple solution as that.

Solution

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);

Answered By – Günter Zöchbauer

Answer Checked By – Senaida (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *