How do I get the directory of the current script, in Dart?

Issue

I want to know what the script’s directory is. I have a command-line Dart script.

Solution

The easiest way to find the script’s directory is to use the path package.

import "package:path/path.dart" show dirname;
import 'dart:io' show Platform;

main() {
  print(dirname(Platform.script.toString()));
}

Put the path package into your pubspec.yaml:

dependencies:
  path: any

And be sure to run pub get to download and link the path package.

Answered By – Seth Ladd

Answer Checked By – Terry (FlutterFixes Volunteer)

Leave a Reply

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