Flutter desktop windows get application path - flutter

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.

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

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.

Try is package: path_provider
set this code in pubspec.yaml
dependencies:
path_provider: ^1.6.27 # get last Version From https://pub.dev/packages/path_provider/install
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

Related

How to use SQlite database using sqflite package in my Flutter App

I'm a beginner in flutter, I want to use SQLite database using sqflite package in my Flutter App,
when I use import 'package:path_provider/path_provider.dart';I have a compilation error saying Target of URI doesn't exist: 'package:path_provider/path_provider.dart'. Try creating the file referenced by the URI, or Try using a URI for a file that does exist.
and when I use
io.Directory documentDirectory = await getApplicationDocumentsDirectory();
I have a compilation error saying
Try correcting the name to the name of an existing method, or defining a method named 'getApplicationDocumentsDirectory'.```
Looks like you need to add path_provider dependency to your pubspec.yaml.
Add this:
dependencies:
...
path_provider: ^2.0.11
Then run
flutter pub get
More info: https://pub.dev/packages/path_provider

Flutter how get local download folder with path_provider

At the moment i build a chat similar in wahtsapp.
I would like upload some local files (pdf, txt, image...).
Like this:
I try to get the local files via path_provider.
But i dont get access to download folder.
My problem:
I don't know how i can access the dowload folder with path_provider.
See my code:
//load local files from download folder
_loadLocalFile() async {
//absoulute path to download file
var file = io.Directory('/storage/emulated/0/Download/')
.listSync(); //use your folder name insted of resume.
//console output -> []
//other try to get download folder
List<io.Directory>? documents = await getExternalStorageDirectories();
//console output -> [Directory: '/storage/emulated/0/Android/data/de.securebuy.securebuy/files', Directory: '/storage/1BEC-0908/Android/data/de.securebuy.securebuy/files']
print(documents.toString());
}
I also add to AndroidManifest following rules:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGEā€/>
Anyone have a idea how i can build a function similar in image?
Is that possible?
Many thx
till now , pathprovider isn't going to let you in downloads file , so if you are on android you can just write the downloads directory , else using android_path_provider it is a null-safety package and you can accsess it via :var downloadsPath = await AndroidPathProvider.downloadsPath; , on ios you can get only the app directory cuase ios makes it's own file for every app thus we use 'pathprovider' with Directory appDocDir = await getApplicationDocumentsDirectory();
Update :
i see what you are trying to achieve and this is not what pathprovider pcakage is used for , you have to use file_picker , read thier docs , i thinks that this is what you need

How to open pdf file in the app using Flutter

I am using flutter_pdfview which is version ^1.0.0+10 to open a PDF file in flutter apps. When my app is in debug mode, there is no error. After I build the app the PDF file cannot open in apps. My app will crash and close suddenly. I have open the permission in AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
How can solve this error?
First you need to create a proguard-rules.pro file at /android/app/ inside there write like this:
#Flutter Wrapper
-keep class com.shockwave.**
Then at /android/app/build.gradle add this:
buildTypes {
release {
signingConfig signingConfigs.release
**minifyEnabled true
useProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'**
}
}
I have this problem only on android but when the app is on production. To fix this I add the permission handler plugin and change the path. Also and very important was add the extension ".pdf" to file after download but before read.
final filename = url.substring(url.lastIndexOf("/") + 1);
var request = await HttpClient().getUrl(Uri.parse(url));
var response = await request.close();
var bytes = await consolidateHttpClientResponseBytes(response);
String dir = (await getExternalStorageDirectory()).path;
File file = new File('$dir/$filename.pdf');
await file.writeAsBytes(bytes);
pathPDF = file.path;
return file;
If you are running debug on an emulator and then release on a real device, this might be your problem:
PdfViewPage(path: '/storage/emulated/0/Download/$pdfName')));
Even if it isn't causing the crash it is a bad idea to hardcode download path like you've done. Different devices can have downloads in different locations. You can use downloads_path_provider to find where downloads is located.
Also, beside declaring the permissions needed in the Manifest you should also ask user for permission and handle the situation if the user doesn't give permission. For that task I suggest using permission_handler.

Unable to load asset (file the app created)

Why does this cause a crash?
final directory = await getApplicationDocumentsDirectory();
final testFile = File("${directory.path}/test.txt");
await testFile.writeAsString("Hello there!", flush: true);
final ByteData bytes = await rootBundle.load(testFile.path);
The rootBundle.load() call causes:
Unhandled Exception: Unable to load asset: /data/user/0/com.flutter.tests/app_flutter/test.txt
BUT if I do a hot reload then it works fine until I restart the app.
I have a dependency path_provider: any in pubspec.yaml which is needed for the getApplicationDocumentsDirectory().
rootBundle only contains assets that were specified by pubspec.yaml and were built with your application. It's not supposed to access files created within file system. Use File or Image classes to open created files.
final bytes = testFile.readAsBytesSync();
The rootBundle contains the resources that were packaged with the
application when it was built. To add resources to the rootBundle for
your application, add them to the assets subsection of the flutter
section of your application's pubspec.yaml manifest.

Unable to get files in Android assets directory

I am trying to find files located in assets. I have .txt, .gif, and .db files that are there in an eclipse android project but do not display when I run the following code:
AssetManager am = getAssets();
try {
String list[] = am.list("/");
I=0;
Str="";
while (list[] != null) {
Str=Str+"\r\n"+list[I];
I++;
}
}
catch(exception e){
}
I get this list:
AndroidManifest.xml
META-INF
assets
classes.dex
com
res
resources.arsc
This list does not include any of the files in assets.
Project properties is currently set to not include assets folder in the build path. When I do include the assets folder in the build path, it gives me the same list with ".." as the first file.
I have tried changing "/" in the third line to "/assets/" but get nothing returned.
Changing the Android Project Build Target between Android 2.3.1 and Android 2.1-update1 appears to have no effect.
Is there a setting in the project properties that is required for files in assets to be included in the build? Is there a different name for the assets directory using AssetManager?
list() takes a relative path within the assets. Seems in your case you need to pass an empty string, am.list("")