In this blog, we are going to learn about File Downloading In Flutter. Downloading a file can serve different purposes for application development. You can make offline applications by saving your files into local storage, as well as enhancing the user experience, etc.
Let’s start the implementation of the File Downloading In Flutter project and get to know how we improvise the user experience of the Flutter application.
Read more about the Flutter app development company.
1 . Add the Flutter Downloader Package Into Pubsec.Yaml Folder
1 2 3 |
flutter_downloader: ^1.6.1 permission_handler: ^8.1.4+2 path_provider: ^2.0.2 |
I have added the “flutter_downloader” latest version, you can add any of the versions according to your compatibility from the Flutter Dev.
I have also added the “permission_handler” package for managing the permissions and “path_provider” for getting the root directory path of the application.
2. Add Path Provider In Manifest
To open the file after downloading it, we need to add some additional changes in the Android Manifest file:
1 2 3 4 5 6 7 8 9 |
<provider android:name="androidx.core.content.FileProvider" android:authorities="com.example.untitled.flutter_downloader.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"/> </provider> |
3. Create A Downloading Task
1 2 3 4 5 6 7 8 9 10 11 12 |
task.taskId = await FlutterDownloader.enqueue( url: // add your file URL here, headers: {"auth": "test_for_sql_encoding"}, savedDir: _localPath, // file directory where you want to store it showNotification: true, openFileFromNotification: true); // I am using following URL --->https://upload.wikimedia.org/wikipedia/commons/6/60/The_Organ_at_Arches_National_Park_Utah_Corrected.jpg // use this in the initState method to initialize FlutterDownloader.registerCallback(downloadCallback); |
4. Load The Downloading Task
1 |
final tasks = await FlutterDownloader.loadTasks(); |
5. Open The file After downloading
1 2 3 4 5 6 7 |
Future<bool> _openDownloadedFile(_TaskInfo? task) { if (task != null) { return FlutterDownloader.open(taskId: task.taskId!); } else { return Future.value(false); } } |
Important NOTES:
-> Add the following permissions to your Android manifest file
1 2 3 4 5 6 7 |
<strong><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /></strong> <strong> <uses-permission android:name="android.permission.INTERNET" /></strong> <strong> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /></strong> <strong> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /></strong> |
-> Steps added here are for Android, but you can check out the steps for iOS as well on the site
Output:
Conclusion:
I hope this blog helps you to understand the basic uses of File Downloading In Flutter as well as the use of the Flutter_downloader package.
Thank you for reading!!
For more information, you can visit the Dev Console.