dartdart-io

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


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.