Daniel Melo
Daniel Melo

Reputation: 131

Flutter desktop windows get application path

I am creating a Flutter Desktop application for windows and I am trying to retrieve the application directory where the exe is located. I tried to use path_provider but can only get the Documents directory. Any help I would appreciate lots.

Upvotes: 13

Views: 12832

Answers (4)

Biloliddin Makhmudov
Biloliddin Makhmudov

Reputation: 112

I had the same issue as you and solved in this way:

import 'package:path/path.dart';
 final directory = dirname(Platform.script.toFilePath());

it is going to give the location of your "run" file

Upvotes: 0

Venceslau Cassinda
Venceslau Cassinda

Reputation: 61

String dir = Directory.current.path;
print(dir);

Directory.current.path (in the dart:io package) returns the current working directory of the project folder (when developing) or the executable directory when running from a release build.

Upvotes: 5

Mabsten
Mabsten

Reputation: 1988

Use Platform.resolvedExecutable (of dart:io).

I have tested it in Flutter/Windows and it works. Strangely, Platform.executable returns null instead. I tell strangely because its type is String not nullable. This unexpected null value can cause crash or errors that are difficult to detect (but in the end Flutter desktop is not in the stable channel).

Upvotes: 21

Mahmoud Salah Eldin
Mahmoud Salah Eldin

Reputation: 2098

Try is package: path_provider

  1. set this code in pubspec.yaml
dependencies:
  path_provider: ^1.6.27 # get last Version From https://pub.dev/packages/path_provider/install
  1. Code
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path; //C:\Users\USER_NAME\AppData\Local\Temp

Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path; //C:\Users\USER_NAME\Documents
//Warning: on debug mode this code get location project but in release mode will get your location app correctly
String dir = Directory.current.path; // PATH_APP

Upvotes: 1

Related Questions