背景
最近有涉及到下载相关的业务,所以去研究了下Flutter社区的下载工具库,pub.dev经过fluttercommunity.dev认证的下载库是flutter_downloader
flutter_downloader提供了下载的一些基础功能。例如任务id映射、任务队列、线程隔离、原生交互
值得一提的是flutter_downloader官方提供的示例教程flutter_downloader/example
这套教程你写了一大段具体业务,主要逻辑是taskId维护、disk/memory缓存同步;个人认为flutter_downloader从易用性角度来说设计的不是很友好,没有达到开箱即用的效果,例如其中的2个问题:
-
标识下载任务的唯一id(taskId)可能在一次pause/resume后变掉了?那怎么进行任务管理?
-
下载回调方法里没有url信息,但是flutter_downloader本地数据库却有,如果想手动绑定taskId和url,难道我要在每一次下载任务回调i/o本地数据库?
为了解决这些问题,便利地把flutter_downloader投入业务,最好进行二次封装。
二次封装后能够做到:
-
跟踪taskId和url的映射关系,调用方通过url管理下载任务,而不是taskId
-
外界弱化对flutter_downloader所维护的下载状态的感知;flutter_downloader维护了
7种下载状态,其实可以完全为调用方屏蔽掉例如undefined、enqueued这些状态 -
在尽量少的I/O前提下,进行disk/memory缓存同步
-
提供便利的下载句柄
-
提供批量下载接口(因flutter_downloader未提供具体下载字节数,目前批量进度 = 已下载任务个数/总任务个数)
为了解决上述的一些问题,基于flutter_downloader,自维护了一个下载器al_downloader,目前已经投入到了我们这边业务中使用了,如有不足,希望大家帮忙指正~
al_downloader
一个基于url的Flutter下载器,支持下载任意类型的文件,并自动管理下载相关的各种事务。
特性
- 通过url管理下载任务
- 简单的下载状态
- 不频繁地I/O
- 提供便利的下载句柄
- 支持批量下载
- 自动管理文件,不需要指定下载路径
- 基于flutter_downloader
集成
原生配置:和flutter_downloader相同
添加下面这行代码到pubspec.yaml中
dependencies:
al_downloader: ^1.8.2
使用命令行运行下面这行代码
flutter packages get
引入下面这行代码来使用al_downloader
import 'package:al_downloader/al_downloader.dart';
使用
ALDownloader
初始化
ALDownloader.initialize();
配置打印
ALDownloader.configurePrint(true, frequentEnabled: false);
下载
ALDownloader.download(url,
directoryPath: directoryPath,
fileName: fileName,
handlerInterface:
ALDownloaderHandlerInterface(progressHandler: (progress) {
debugPrint(
'ALDownloader | 下载进度 = $progress, url = $url\n');
}, succeededHandler: () {
debugPrint('ALDownloader | 下载成功, url = $url\n');
}, failedHandler: () {
debugPrint('ALDownloader | 下载失败, url = $url\n');
}, pausedHandler: () {
debugPrint('ALDownloader | 下载暂停, url = $url\n');
}));
添加一个下载句柄池
ALDownloader.addHandlerInterface(
ALDownloaderHandlerInterface(progressHandler: (progress) {
debugPrint(
'ALDownloader | 下载进度 = $progress, url = $url\n');
}, succeededHandler: () {
debugPrint('ALDownloader | 下载成功, url = $url\n');
}, failedHandler: () {
debugPrint('ALDownloader | 下载失败, url = $url\n');
}, pausedHandler: () {
debugPrint('ALDownloader | 下载暂停, url = $url\n');
}),
url);
添加一个持久下载句柄池
ALDownloader.addForeverHandlerInterface(
ALDownloaderHandlerInterface(progressHandler: (progress) {
debugPrint(
'ALDownloader | 下载进度 = $progress, url = $url\n');
}, succeededHandler: () {
debugPrint('ALDownloader | 下载成功, url = $url\n');
}, failedHandler: () {
debugPrint('ALDownloader | 下载失败, url = $url\n');
}, pausedHandler: () {
debugPrint('ALDownloader | 下载暂停, url = $url\n');
}),
url);
移除下载句柄池
ALDownloader.removeHandlerInterfaceForUrl(url);
暂停下载
/// 停止下载,不删除未下载完成的数据
ALDownloader.pause(url);
取消下载
/// 停止下载,删除未下载完成的数据
ALDownloader.cancel(url);
移除下载
/// 删除下载,删除所有数据
ALDownloader.remove(url);
获取下载状态
final status = await ALDownloader.getStatusForUrl(url);
获取下载进度
final progress = await ALDownloader.getProgressForUrl(url);
获取任务
final task = await ALDownloader.getTaskForUrl(url);
获取全部任务
final tasks = await ALDownloader.tasks;
ALDownloaderBatcher
批量下载
ALDownloaderBatcher.download(urls,
handlerInterface:
ALDownloaderHandlerInterface(progressHandler: (progress) {
debugPrint('ALDownloader | 批量 | 下载进度 = $progress\n');
}, succeededHandler: () {
debugPrint('ALDownloader | 批量 | 下载成功\n');
}, failedHandler: () {
debugPrint('ALDownloader | 批量 | 下载失败\n');
}, pausedHandler: () {
debugPrint('ALDownloader | 批量 | 下载暂停\n');
}));
对批量下载添加一个下载句柄池
ALDownloaderBatcher.addHandlerInterface(
ALDownloaderHandlerInterface(progressHandler: (progress) {
debugPrint('ALDownloader | 批量 | 下载进度 = $progress\n');
}, succeededHandler: () {
debugPrint('ALDownloader | 批量 | 下载成功\n');
}, failedHandler: () {
debugPrint('ALDownloader | 批量 | 下载失败\n');
}, pausedHandler: () {
debugPrint('ALDownloader | 批量 | 下载暂停\n');
}),
urls);
获取一组url的下载状态
final status = ALDownloaderBatcher.getStatusForUrls(urls);
获取一组任务
final tasks = await ALDownloaderBatcher.getTasksForUrls(urls);
ALDownloaderFileManager - 基于url的文件管理器
final physicalFilePath =
await ALDownloaderFileManager.getPhysicalFilePathForUrl(url);
debugPrint(
'ALDownloader | 获取[url]的物理文件路径, url = $url, path = $physicalFilePath\n');
提示:
1. 如果持久化文件被一些异常方式删除了,比如某些业务代码删除了缓存文件夹,调用[remove],然后调用[download]重新下载来解决这个问题
Example的主要文件
iOS
Android
贡献
欢迎贡献!
如果你有任何问题、建议或想法,欢迎随时提出issue或PR!
Maintainer: jackleemeta (jackleemeta@outlook.com)