What is the main difference between await getTemporaryDirectory();
and await getApplicationSupportDirectory() with Flutter Path provider package.
It is mentioned that what is the underlying calls made for the function:
getTemporaryDirectory , getApplicationSupportDirectory
Considering the example of Android, the path returned by both methods should be used for storing the app-related data.
But there is a minor difference that for the files inside the path returned by getTemporaryDirectory , the system will automatically delete files in this directory as disk space is needed elsewhere on the device. The system will always delete older files first, based on the lastModifiedTime.
Storage permission won't be required for both the methods.
As per documentation :
getTemporaryDirectory :
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. This does not
return a new temporary directory. Instead, the caller is responsible
for creating (and cleaning up) files or directories within this
directory. This directory is scoped to the calling application.
On iOS, this uses the NSCachesDirectory API.
On Android, this uses the getCacheDir API on the context.
getApplicationSupportDirectory :
Path to a directory where the application may place application
support files.
Use this for files you don’t want exposed to the user. Your app should
not use this directory for user data files.
On iOS, this uses the NSApplicationSupportDirectory API. If this
directory does not exist, it is created automatically.
On Android, this function uses the getFilesDir API on the context.
Related
Is it always safe to use the path "/storage/emulated/0/{MY APP NAME}" to store the files ?
I Want to store files in the external storage of the android device and I want to store it in a folder named after the app name this folder needs to be located in the external storage.
Have you tried the package path_provider https://pub.dev/packages/path_provider/install?
It comes with the methods getExternalStorageDirectory() and getApplicationDocumentsDirectory() which might be what you are looking for. As far as I know different os types are automatically considered as well.
try the same package of ext_storage named android_external_storage its the same concept putting the downloaded data for example to the download folder.
Check this thread.
The directory an app can access on sdcard looks like /storage/1718-0217/Android/data/com.example.app_name/files. You can get this with the code below.
(await getExternalStorageDirectories())?[1].path;
One app should only access some restricted directories on sdcard. Or you should use Permission.manageExternalStorage.request(), it is strongly restricted and always return false.
Fortunately, per-app directories can also be scanned by other services such like media.
What is the best way to use assets in Flutter , for example if i have a file for app configuration , should I store the file by getting the app directory using the path_provider plugin -without using assets- and store it ?, or should I add the file to my program folder -add the file to my assets- ?
the same question if I have a small Sqlite database.
and which of these methods is faster , and which is more secure ?
Assets are files that you add to your app during development. You can load them with rootBundle.load() or rootBundle.loadString() but you cannot modify or delete them.
In the app's directory you can store any files that your app downloads or generates from the internet while running. These files can then be opened, deleted, modified, etc. To access your app directory you need the package path_provider, which tells you the path to your app folder.
A sqlite database is normally stored in the app directory. An example package would be here sqflite.
For speed and security I can't make a difference. An app directory is designed so that only the app can access it. Assets are a part of the app, the application file can theoretically be unpacked by anyone. Therefore I would at least not store secret things in the assets.
Well, if by app configuration you mean the user's settings you can use Sqlite, SharedPreferences or Hive (Hive shows a benchmark that says that it is faster than SharedPreferences).
I believe that assets folder is used to store some common files for the app, like images, icons, fonts, etc. And I think that isn't recommended to store files with some kind of config file, mainly with critical info about the app configuration.
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!
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.
Pretty much everyone and every tutorial seems to store it's private app data in the documents directory.
But in my image editing app I want the user to be able to use iTunes file sharing. The problem: It exposes everything in documents to the user, and he can accidentally delete important private app data folders like the SQLite database.
Apple recommends this:
If you do not want files to be shared
with the user, put them in your
application’s Library directory. If
none of the standard Library
subdirectories are appropriate, create
a private directory inside the Library
directory and give it the name of your
application’s bundle ID. You can then
use that directory to store any files
that are private to your application.
I have never heard of that Library directory. Now the question is: How can I access this directory? Where is it? Will it be backed up, just like the documents directory when the user syncs with iTunes?
Use NSLibraryDirectory in lieu of NSDocumentDirectory (in those examples you speak of) to get the library directory and then create a directory in there using NSFileManager.
Read this for more information on the application directory structure. The Library directory is backed up except the Caches directory.