flutter_downloader 1.0.1 flutter_downloader: ^1.0.1 copied to clipboard
A plugin for creating and managing download tasks. Supports iOS and Android.
Flutter Downloader #
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
ios
project (fileRunner.xcworkspace
) in Xcode. -
Enable background mode.
- Add
sqlite
library.
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>
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
(insideapplication
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)
Usage #
import 'package:flutter_downloader/flutter_downloader.dart';
Initialize plugin:
FlutterDownloader.initialize(
maxConcurrentTasks: 3, // config the maximum number of tasks running at a moment
messages: {....} // localize messages for Android notification
);
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)
openFileFromNotification: true, // click on notification to open downloaded file (for Android)
);
Update download progress:
FlutterDownloader.registerCallback((id, status, progress) {
// code to update your UI
});
Load all tasks:
final tasks = await FlutterDownloader.loadTasks();
Load tasks with conditions:
final tasks = await FlutterDownloader.loadTasksWithRawQuery(query: query);
- Note: In order to parse data into
DownloadTask
object successfully, you should load data with all fields from DB (in the other word, use:SELECT *
). For example:
SELECT * FROM task WHERE status=3
- Note: the following is the schema of
task
table where this plugin stores tasks information
CREATE TABLE `task` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`task_id` VARCHAR ( 256 ),
`url` TEXT,
`status` INTEGER DEFAULT 0,
`progress` INTEGER DEFAULT 0,
`file_name` TEXT,
`saved_dir` TEXT,
`resumable` TINYINT DEFAULT 0,
`headers` TEXT,
`show_notification` TINYINT DEFAULT 0,
`open_file_from_notification` TINYINT DEFAULT 0,
`time_created` INTEGER DEFAULT 0
);
Cancel a task:
FlutterDownloader.cancel(taskId: taskId);
Cancel all tasks:
FlutterDownloader.cancelAll();
Pause a task:
FlutterDownloader.pause(taskId: taskId);
Resume a task:
FlutterDownloader.resume(taskId: taskId);
- Note:
resume()
will return a newtaskId
corresponding to a new background task that is created to continue the download process. You should replace the originaltaskId
(that is marked aspaused
status) by this newtaskId
to continue tracking the download progress.
Retry a failed task:
FlutterDownloader.retry(taskId: taskId);
- Note:
retry()
will return a newtaskId
(likeresume()
)
Open and preview a downloaded file:
FlutterDownloader.open(taskId: taskId);
- Note: in Android, you can only open a downloaded file if it is placed in the external storage and there's at least one application that can read that file type on your device.
Bugs/Requests #
If you encounter any problems feel free to open an issue. If you feel the library is missing a feature, please raise a ticket on Github. Pull request are also welcome.