How to open/find the file created by path_provider? - flutter

I'm a beginner at flutter and dart and had some issues with file handling using flutter.
I'm trying to store data locally in text files for my flutter application, and came across the standard tutorial on flutter's website which used path_provider and dart:io. It had instructions on getting the path and reading and writing to a file, however this didn't seem to work for files I added to the project. The only way to store to files was to first write using the inbuilt functions, then reading this newly created file. I can't add my own file and read it using flutter.
I also can't open the file created by flutter as I don't know where its stored. The path given by getApplicationDocumentsDirectory() returns the path /data/user/0/com.example.daily_challenges/app_flutter, and I don't know where to find this in my project.
In summary: How do I read a pre-existing file and how do I view the file created automatically by flutter.
Thanks :)

You can access the files from a physical device or simulator through Device File Explorer in Android Studio. Follow this guide

You won't be able to access getApplicationDocumentsDirectory().
If you are using android device, you can try to store it in getExternalStorageDirectory(). There's no equivalent in IOS though.

If you are running in a physical device. Open Device File Explorer in Android Studio and you can find the file under
data/data/your app package/app_flutter/fileName.txt
For example,
data/data/com.example.file_example/app_flutter/example.txt
And if you want to read the pre-existing file, you not need to anything specific, if you give the same file name, if not exist, it will create one, otherwise it will return the old one.
final File file = File(filePath);
file.writeAsStringSync('${text}', mode: FileMode.append);
For write, you consider using FileMode if you want to append text to the existing file. Or else by default overwrite will happen.
For read, you can consider this
final File file = File(filePath);
String text = await file.readAsString();

Just use Device File Explorer from Android Studio.
But the weird thing, path_provider gives you path like /data/user/0/your_app_id/..., but in fact all files are located in /data/data/your_app_id/..., as mentioned in previous answer.

Related

Is there a way to preview/open a file from a URL within the macCatalyst app without saving it in File system?

I'm trying to load/open/preview the files within the app but I don't want them to download and be saved in the file system to open them on MacCatalyst. I tried using QuickLook/QLPreviewController but it only works for files already stored in the File system.
I'm fairly new to MacCatalyst, any help is appreciated.

Open generated file (Uint8list) in default application in Flutter

My application is generating excel and pdf reports. They are generated in Uint8list.
Is there a way to open such files in default application?
I tried open_file package, however, it requires a file path while my file is in memory.
same with url_launcher.
Also I tried saving the file then using open_file, but it doesn't work on web as file can't be saved.
I solved the issue by using printing.dat package. it has a PdfPreview that takes Uin8list and shows a preview with so many other options.

How to read a file in Flutter server side

I've looked up many topic but none seems to do what i need.
I am working on a website with flutter and i need to read a file thats located on the server.
Is this in any way possible?
Edit: The contents in this file change, so i have to update the contents on the website.
If you want to be able to access the file as if it was locally stored, then it needs to be registered under your assets in your pubspec.yaml & should be included in the project itself. Otherwise, you will have to fetch the file using something like DIO to fetch it from wherever it is hosted.

Download offline map at runtime in Flutter using Mapbox?

We have a flutter application where we want to use map functionality in offline mode.
Mapbox provides functionality in the flutter to use offline maps but .db file which contains offline data needs to be saved in the project at build time.
How to achieve the same at runtime?
also open to suggestions to use any other map service providers that works in both online and offline mode.
If you want to store a file on the device please read the official Flutter documentation about Reading and Writing Files
Essentially you need to add path_provider package to your project in order to retrieve the standard cache folder for every device and then simply save you file in it.
This is a my application similar behaviour code
//Get an available temporary dir on device using path_provider package
final Directory tempDir = await getTemporaryDirectory();
//Create a path with file name
final String tempPath = tempDir.path + '/' + 'yourFileName.db';
//Write file on device disk
final File file = File(tempPath);
await writeAsBytesSync(fileContent); //If it is a string use writeAsStringAsync
print('File written on disk');
Then using the file path you can simply read it from disk using readAsByteAsync method.
Remember that in the sample we are using getTemporaryDirectory() and as the documentation tell us
Path to the temporary directory on the device that is not backed up and is suitable for storing caches of downloaded files.
Files in this directory may be cleared at any time.
I would recommend to check out this issue on the github page of the flutter-mapbox-gl repository:
https://github.com/tobrun/flutter-mapbox-gl/issues/88
See the comment of vinceKruger:
https://github.com/tobrun/flutter-mapbox-gl/issues/88#issuecomment-559380534
It is not an official way, but seems to work!

Write to a file in local storage flutter

I know about the path_provider package but it doesn't do what i want, or maybe I'm not just using it right. I read after so many trials and errors that the getApplicationDocumentsDirectory() returns a directory that is accessible only by the app itself, but what if i want to write to a phone's local document directory or so and be able to view the file in my file explorer later on?
If you want to save where the file explorer reaches, you must use the method getExternalStorageDirectory(). It only works in Android and you'll need READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions.
Actually, you're able to find the files saved using getApplicationDocumentsDirectory() and getTemporaryDirectory() as well, but you'd need root access.