flutter_downloader 0.1.1 copy "flutter_downloader: ^0.1.1" to clipboard
flutter_downloader: ^0.1.1 copied to clipboard

outdated

A plugin for creating and managing download tasks. Supports iOS and Android.

Flutter Downloader #

pub package

A plugin for creating and managing download tasks. Supports iOS and Android.

This plugin is based on WorkManager in Android and NSURLSessionDownloadTask in iOS to run download task in background mode.

iOS integration #

  • Open Xcode. Enable background mode.
  • Add following code to your AppDelegate (this method will be called when an URL session finished its work while your app is not running):
- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)(void))completionHandler {
    completionHandler();
}

Note: If you want to download file with HTTP request, you need to disable Apple Transport Security (ATS) feature.

  • Disable ATS for a specific domain only: (add following codes to the end of your Info.plist file)
<key>NSAppTransportSecurity</key>
<dict>
  <key>NSExceptionDomains</key>
  <dict>
    <key>www.yourserver.com</key>
    <dict>
      <!-- add this key to enable subdomains such as sub.yourserver.com -->
      <key>NSIncludesSubdomains</key>
      <true/>
      <!-- add this key to allow standard HTTP requests, thus negating the ATS -->
      <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
      <true/>
      <!-- add this key to specify the minimum TLS version to accept -->
      <key>NSTemporaryExceptionMinimumTLSVersion</key>
      <string>TLSv1.1</string>
    </dict>
  </dict>
</dict>
  • Completely disable ATS: (add following codes to the end of your Info.plist file)
<key>NSAppTransportSecurity</key>  
<dict>  
    <key>NSAllowsArbitraryLoads</key><true/>  
</dict>

Note: configure the maximum of concurrent download tasks

AppDelegate.m

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

  // config the maximum of concurrent download tasks
  [FlutterDownloaderPlugin setMaximumConcurrentTask: 2];
  
  [GeneratedPluginRegistrant registerWithRegistry:self];
  // Override point for customization after application launch.
  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

Android integration #

In order to handle click action on notification to open the downloaded file on Android, you need to add some additional configurations:

  • add the following codes to your AndroidManifest.xml (inside application tag):
<provider
    android:name="vn.hunghd.flutterdownloader.DownloadedFileProvider"
    android:authorities="${applicationId}.flutter_downloader.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths"/>
</provider>
  • you have to save your downloaded files in external storage (where the other applications have permission to read your files)

Note: The downloaded files are only able to be opened if your device has at least an application that can read these file types (mp3, pdf, etc)

Note: configure the maximum of concurrent download tasks

MainActivity.java

@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // configure the maximum of concurrent download tasks
    FlutterDownloaderPlugin.maximumConcurrentTask = 2;
    
    GeneratedPluginRegistrant.registerWith(this);
  }

Usage #

import 'package:flutter_downloader/flutter_downloader.dart';

To create new download task:

final taskId = await FlutterDownloader.enqueue(
  url: 'your download link', 
  savedDir: 'the path of directory where you want to save downloaded files', 
  showNotification: true, // show download progress in status bar (for Android)
  clickToOpenDownloadedFile: true, // click on notification to open downloaded file (for Android)
);

To update download progress:

FlutterDownloader.registerCallback((id, status, progress) {
  // code to update your UI
});

To load the status of download tasks:

final tasks = await FlutterDownloader.loadTasks();

To cancel a task:

FlutterDownloader.cancel(taskId: taskId);

To cancel all tasks:

FlutterDownloader.cancelAll();
1388
likes
30
pub points
99%
popularity

Publisher

verified publisherfluttercommunity.dev

A plugin for creating and managing download tasks. Supports iOS and Android.

Repository (GitHub)
View/report issues

License

BSD-3-Clause (LICENSE)

Dependencies

flutter

More

Packages that depend on flutter_downloader