How to open/find the file created by path_provider?

Viewed 2713

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

4 Answers

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.

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

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