user254340
user254340

Reputation: 517

flutter file storage application state

I am attempting to read/write a file (json file) that will store data for my app. I am a little confused as to where I should keep such a file in my project. On one hand it seems that I should create an asset and follow the instructions seen here:

https://flutter.dev/docs/development/ui/assets-and-images#loading-text-assets

on the other hand it seems that I should use something called the “Documents directory”

https://flutter.dev/docs/cookbook/persistence/reading-writing-files#1-find-the-correct-local-path

Following the example in the first link i am able to load a json file into my app and read it using the

loadAsset(‘assets/asset.json’) function

From this I am able to read a file just fine in this location, but I am not able to figure out how to write to this file.

Now using the second link I am able to write a file using the “Documents directory”. However, when I try to locate the file that was written I cannot find the file anywhere on my hardrive.

So my first question is if I want to store a file that holds my application state where do I do it? Should it be an asset? Or should it be stored in the “Documents directory”?

My guess is that assets are for read only assets such a static config file and images and the “Documents directory” is how I am suppose to store my application data. Is this correct?

If so how do I get a file into a my “Documents directory” and where is it in my project structure when I am debugging my app?

When i print the location of the file that was written it is:

'/data/user/0/com.example.flutter_expansion_tile_05/app_flutter/tree.json'

which is not a valid location on my PC.

I am guessing that the "Documents directory" (AppData directory) only has meaning when running on my phone and not when running on my PC in debugging mode???

Upvotes: 0

Views: 921

Answers (1)

Adithya Shetty
Adithya Shetty

Reputation: 1287

You should use the "Documents directory" as you said, which is the preferred method. This reads/writes the file to the device local storage where your app is installed and not on your computer local storage.


/data/user/0/com.example.flutter_expansion_tile_05/app_flutter/tree.json

The above path is the path to the file in the device where the app is installed.

Now if you are using Android Studio and Emulator then you can view the emulator files from Android Studio itself.

to do this open the Device File Explorer button or go to

View --> Tool Windows --> Device File Explorer

and navigate to the given path.

https://developer.android.com/studio/debug/device-file-explorer

Upvotes: 4

Related Questions