A Flutter downloader library which handles download in separate thread with background processing. This works in both Android and IOS.
- Manage Downloads By tasks
- Able to listen to status and progress changes
- Partial Download Feature
- Able to download, resume, retry, cancel or remove tasks
- Android
- IOS
In your pubspec.yaml
file add:
dependencies:
background_downloader: any
Then, in your code import:
import 'package:background_downloader/background_downloader.dart';
Inside your AndroidManifest.xml
file, add the following:
Add, permisssions:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="28"/>
Add, external storage read and write permission for sdk version less than 28. Optional, if you need to install packages/apks, add:
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
Under <application> ... <\application>
Optional, Add below code to open file from notification:
<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>
Optional, Add below code to configure maximum number of concurrent download tasks
<!-- Begin FlutterDownloader customization -->
<!-- disable default Initializer -->
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
tools:node="merge">
<meta-data
android:name="androidx.work.WorkManagerInitializer"
android:value="androidx.startup"
tools:node="remove" />
</provider>
<!-- declare customized Initializer -->
<provider
android:name="vn.hunghd.flutterdownloader.FlutterDownloaderInitializer"
android:authorities="${applicationId}.flutter-downloader-init"
android:exported="false">
<!-- changes this number to configure the maximum number of concurrent tasks -->
<meta-data
android:name="vn.hunghd.flutterdownloader.MAX_CONCURRENT_TASKS"
android:value="5" />
</provider>
<!-- End FlutterDownloader customization -->
Inside your AppDelegate.swift
file, replace the current code with the following code:
import Flutter
import UIKit
import flutter_downloader
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
FlutterDownloaderPlugin.setPluginRegistrantCallback(registerPlugins)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
private func registerPlugins(registry: FlutterPluginRegistry) {
if (!registry.hasPlugin("FlutterDownloaderPlugin")) {
FlutterDownloaderPlugin.register(with: registry.registrar(forPlugin: "FlutterDownloaderPlugin")!)
}
}
Please refer to /example
folder for a working example.
In your main.dart
file, add the following lines of code before runApp
method:
WidgetsFlutterBinding.ensureInitialized();
await DownloadManager.initialize(debug: true);
Now, You can use the DownloadManager
class.
final DownloadManager _downloadManager = DownloadManager(
url: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/SubaruOutbackOnStreetAndDirt.mp4');
_downloadManage.initializeDownload();
_downloadManage.dispose();
Place the above code in you initState()
of your dart code.
DownloadModel _downloadTask = DownloadModel.initial();
_downloadManager.getDownloadStream()?.listen(
(event) {
log('event => $event');
setState(() {
_downloadTask = event;
});
},
);
await _downloadManager.startDownload(_downloadTask.taskID)
await _downloadManager.pauseDownload(_downloadTask.taskID)
await _downloadManager.resumeDownload(_downloadTask.taskID)
await _downloadManager.removeDownload(_downloadTask.taskID)
await _downloadManager.cancelDownload(_downloadTask.taskID)
await _downloadManager.retryDownload(_downloadTask.taskID)
bool isDownloaded = await _downloadManager.openFile(_downloadTask.taskID);
#TODO
- Multiple downloads at once
- Cannot open downloaded files in Android