I want to save a download a video, save it into a temporary file that will automatically disappear when the app is closed. Is there a convenient way to do so in Flutter?
I'm aware that we can use path_provider package to generate a file in temporary folder, but that file will not disappear after user close the app.
try flutter_cache_manager, you can download the file in cache and remove it from cache:
getFileStream(url) returns a stream with the first result being the cached file and later optionally the downloaded file.
getFileStream(url, withProgress: true) when you set withProgress on true, this stream will also emit DownloadProgress when the file is not found in the cache.
downloadFile(url) directly downloads from the web.
getFileFromCache only retrieves from cache and returns no file when the file is not in the cache.
putFile gives the option to put a new file into the cache without downloading it.
removeFile removes a file from the cache.
emptyCache removes all files from the cache.
If you want to save anything in Flutter, i think key-value storing is the best way to do. In Flutter there is a package called shared_preferences which is help you to store key-value data on disc.
I copied the official websites of the shared_preferences package. On these websites you can read a lot about this.
https://flutter.dev/docs/cookbook/persistence/key-value
https://pub.dev/packages/shared_preferences
Related
I am using dio and path_provider to save pdf from url, I am going to show that pdf to user using open_file. But I have problem with saving path.
Where I can save file temporary, show that file, and after some time or when user will not look at the app delete file? File can be even deleted after 2 days. Time when pdf will be removed is not that important.
It can be even longer time, but sooner or later it must happen. This is in order to not have old pdf's stored on the phone in case of pdf's change on the web
Right now it is my path:
var tempDir = await getTemporaryDirectory();
String fullPath = "${tempDir.path}/test.pdf";
edit: there is also a second option, to check if something is in this place saved, if yes, then remove it and download again, but this place can not be shown to a users, I found "getApplicationDocumentsDirectory", this allows me to save pdf in place where users can not read a pdf.
After that reaserch my question will be, If I download my pdf in a way as you can see above it will stay in that place and will never be moved? Now I need to only creade a methode that will see if my pdf is there end will remove it? How to extract name of my pdf I will need that?
I guess you have selected the right path if you just needed to delete the file for the space on device because when you use the 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.
But if you want to check if the file changed or not you can send in the response of the file a flag that contains the last updated date and this value can be the file name prefix, so when ever you open the app you can check if the date has changed you will delete the old one manually and store the new one.
If you follow this solution it will be no need to use getTemporaryDirectory() you have to use the getApplicationDocumentsDirectory()
I am using the library flutter_cached_pdfview. I am using PDF().fromUrl instead of PDF().cachedFromUrl, and expecting that PDF().fromUrl will never cache the file. I want to download the file everytime because the PDF is constantly changing on the server. How to force PDF().fromUrl to re-download the file everytime.
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.
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!