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.