How to Use Flutter Downloader?

Topics Covered

Flutter Downloader elevates Flutter app file downloading, employing WorkManager on Android and NSURLSessionDownloadTask on iOS for background tasks. Despite its prowess, ongoing discussions focus on redesigning the plugin due to challenges with Android 11's external storage APIs. The Flutter Downloader team actively seeks contributions and feedback from the Flutter community to enhance the plugin's design and address current implementation issues.

iOS integration

Before starting the integration make sure the required dependencies of Flutter and the downloader plugin are added in the project.

Essential configuration:

You need to open your iOS project in Xcode before continuing.

  1. Enable background mode. Enable background mode

  2. Add the SQLite library. Add the SQLite library1 Add the SQLite library2

  3. Configure AppDelegate: Objective-C:

Or Swift:

Optional configuration:

i. Support HTTP request:

  • You must turn off Apple Transport Security (ATS) if you want to download a file using an HTTP request. There are 2 choices.
  1. Disable ATS just for a particular domain: (Edit your Info.plist file to include the following code)
  1. Disable ATS entirely. The following should be put in your Info.plist file.

ii. Define the max number of concurrent tasks:

  • By default, the Flutter Downloader plugin allows you to download three tasks at a time (if more than three tasks are enqueued, only three jobs are running; the remaining tasks are in the waiting state). This number can be changed by adding the following code to your Info.plist file.

iii. Localize notification messages:

  • If all files are downloaded when your application is not running in the foreground, the Flutter downloader plugin will send a notice message to the user. By default, this message is in English. This message can be localized by adding and localizing the following message to the Info.plist file. (You can learn more about Info. list localization at this link.)

NOTE: The Flutter downloader plugin in iOS only supports saving files in NSDocumentDirectory.

Andriod Integration

You do not require to do anything special to enable the Flutter downloader plugin to work on Android.

There are, however, a few optional parameters that you may choose to configure.

Open Downloaded File From Notification:

Add the following code to AndroidManifest.xml to make tapping on the notification on an Android device launch the downloaded file:

i. Downloaded files must be saved to an external storage device where other apps can access them.. ii. Only devices with at least one programme capable of reading these file kinds (MP3, PDF, etc.) can open the downloaded files.

Configure Maximum Number of Concurrent Download Tasks:

The FLutter Downloader plugin depends on the WorkManager library, and WorkManager configures the maximum number of jobs running at once based on the number of processors available. By including the following code in your AndroidManifest.xml file, you can configure a fixed number for this configuration:

Note: add xmlns:tools="http://schemas.android.com/tools" line in the manifest tag otherwise it will lead to an error at the time of gradle build.

Localize strings in Notifications:

Texts in download progress alerts can be localized by translating the subsequent messages.

Install .apk files:

Your application needs REQUEST_INSTALL_PACKAGES permission to open and install.apk files. 'AndroidManifest.xml' should have the following additions:

Usage

Here are the steps to use the Flutter downloader package.

  1. Add the Flutter Downloader dependency to your pubspec.yaml file and run flutter pub get.
  1. Import the Flutter Downloader package in your Dart file:
  1. Initialize Flutter Downloader in your application. This step is usually done in the main() function or during app initialization. Add the following line of code:
  1. To download a file, you can use the FlutterDownloader.enqueue method. For example:
  1. You can listen to download progress and status using the FlutterDownloader.registerCallback method. This allows you to update UI elements, such as progress bars, based on the download progress and handle download completion. For example:

How to Implement?

The steps shown in the above section are not enough. We must implement the flutter downloader we have to handle the communication between two isolates.

To do it, we need to register the port:

The initiate of the code above consists of the registration of the port with the name downloader_send_port.

The UI of the Flutter app is rendered on the main isolate. While the downloading events happens in background isolate. So we have to handle the communication between the main isolate (which handles the UI rendering) and the background isolate (which handles the download event).

Once the download process is done we need to remove the port which is done in the dispose method by calling IsolateNameServer.removePortNameMapping('downloader_send_port');.

downloadCallback is the callback where @pragma('vm: entry-point) is placed above the callback function to avoid tree shaking in release mode for Android.

Example

Here's an example app.

You can find the whole source code here

main.dart

file_listing_screen.dart

  • file_listing_screen.dart is used to show the list of the urls. When the user will tap on any of the tiles, the user will be navigated to the file_download_screen.dart.

file_listing_screen

file_download_screen

  • file_download_screen.dart is used to down the file by clicking on the Download button.
  • When the user clicks on the Download button isDownloading flag will be true and the view will be changed with progress bar, download status string, and a row that contains the control button that includes pause, resume and cancel download button.

Download button1 Download button2

Conclusion

  • Flutter Downloader is a powerful package for Flutter developers that simplifies the process of downloading files from the internet in mobile applications.
  • Flutter Downloader package enables developers to effortlessly download and manage various file types, such as images, videos, and documents, providing a seamless user experience.
  • Flutter Downloader allows for the handling of complex download tasks, including pause and resume functionalities, enhancing control and flexibility for users.
  • The package offers intuitive progress indicators, keeping users informed about the download status and progress, contributing to a more engaging user interface.
  • Flutter Downloader supports background downloading, enabling users to continue using the app while files are being downloaded, ensuring uninterrupted multitasking.
  • By leveraging Flutter Downloader, developers can create feature-rich applications that provide offline access to content, enhancing the overall functionality and interactivity.
  • With its ease of use and comprehensive features, Flutter Downloader proves to be an invaluable tool for Flutter developers seeking to integrate file-downloading capabilities into their applications.