Download offline map at runtime in Flutter using Mapbox? - flutter

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!

Related

Difference between Application Documents Directory and Support Directory in path_provider?

I am new to path_provider. I am making a music app and want to download an mp3 file but I don't know which is the right way to download the file within the application.
I want to store the file with application storage so users can not access it directly.
so where i can store it?
final dir = await getApplicationDocumentsDirectory();
final dir2 = await getApplicationSupportDirectory();
print(dir.path);
print(dir2.path);
<---------- Output of path ----------->
/data/user/0/com.oraysa/app_flutter.
/data/user/0/com.oraysa/files
together with this one more question is how can I access downloaded files ( get info of all downloaded files). how can I find the same file so next time I don't have to download it again, instead just directly load it from storage?

What is the difference between getTemporaryDirectory() and getApplicationSupportDirectory()?

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.

Store files from flutter application to external storage

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.

How to save file temporarily in Flutter?

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

How to open/find the file created by path_provider?

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.