download_assets 包使 Flutter 应用程序能够检索并本地存储资产到应用的仓库中,使其可用于离线使用。它支持从远程 URL 下载文件并将其保存到设备的文件系统中,这在缓存图像、文档或配置文件等资源时特别有用。该包可在 Android、iOS、macOS、Windows 和 Linux 等多个平台上运行,确保行为一致。它通过异步处理下载并自动管理文件路径,简化了资产管理工作。
适合场景
离线内容访问
缓存远程资源
预加载应用资源
动态内容更新
资源回退机制
主要特点
跨平台支持
异步下载
本地文件存储
自动路径管理
对各种文件类型的支持
适合对象
需要离线功能的应用程序
内容密集型应用
具有动态资源交付功能的应用程序
管理远程资源的开发者
需要缓存数据的项目
使用建议
移动设备上需要互联网权限
需要磁盘空间管理
文件命名冲突可能
无内置重试逻辑
手动清理已下载的文件
原始 README
以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。
Download_assets
About
The download_assets package is a powerful library that facilitates the process of downloading assets
into the application. With this library, you can easily download files, images, videos, and other
resources in your app, providing a smooth and efficient user experience.
Features
init
Init method for setting up the assetsDir, which is required to be called during app
initialization.
await downloadAssetsController.init();
startDownload
Starts the asset download process.
assetsUrls: A list of [AssetUrl] objects representing each file to be downloaded.
uncompressDelegates: A list of custom decompression delegates for different types of files, such
as ZIP, RAR, etc (optional).
onStartUnzipping: Called right before the start of the uncompressing process (optional).
onProgress: Called after each iteration returning the current progress (optional). The double parameter ranges from 0 to 1, where 1 indicates the completion of the download process.
onDone: Called when all files have been downloaded and uncompressed (optional).
onCancel: Cancels the ongoing download (optional).
requestQueryParams: Query params to be used in the request (optional).
requestExtraHeaders: Extra headers to be added in the request (optional).
checkSize: Specifies if the size of the file should be checked first before starting download (optional).
Uncompress delegates are responsible for implementing the logic to uncompress different types of
files, such as ZIP, RAR, etc. By providing a list of delegates, it becomes possible to support
multiple file formats during the download.
To implement your own uncompression delegate, you should implement the interface below.
/// Abstract class representing a delegate for asset decompression.abstractclassUncompressDelegate{
constUncompressDelegate();
/// Gets the file extension associated with the delegate.Stringgetextension;
/// uncompress the asset located at [compressedFilePath] to the specified [assetsDir]./// [compressedFilePath] -> The path to the compressed asset file./// [assetsDir] -> The directory where the uncompressed asset should be stored.Futureuncompress(String compressedFilePath, String assetsDir);
}
Note: This kind of delegate is essential for enabling the download process to handle various
file formats by implementing the necessary decompression logic. This flexibility allows users to
download and work with a wide range of compressed file types effortlessly. UnzipDelegate() is
already included, which handles the decompression of ZIP files.
Example
You can find an example here: https://github.com/edjostenes/download_assets/tree/master/example
Contributing
Contributions are welcome! If you encounter any issues or have suggestions for improvement,
please create an issue on the GitHub repository.