Flutter文件下载管理插件shorebird_downloader的使用
Flutter文件下载管理插件shorebird_downloader的使用
shorebird_downloader
这是一款支持Shorebird定制补丁下载地址并监控下载进度的插件。
当然,除了下载功能外,您还需要通过shorebird_code_push
插件获取与Shorebird相关的其他补丁状态。
⚠️ 目前,在通过此库下载Android补丁并启动时会报错。-> https://github.com/shorebirdtech/shorebird/issues/1449
如何安装
flutter pub add shorebird_downloader
如何使用
现在您可以使用以下方法来检测补丁、下载、弹出提示重启等。
ShorebirdCheckDownloader(
customShowUpdateDialog: (currentPatchNumber, nextPatchNumber) =>
ShorebirdCheckDownloader.showUpdateDialog(
Get.context!,
currentPatchNumber,
nextPatchNumber,
),
).checkPatch();
final downloader =
ShorebirdUrlDownloader(appid: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx');
await downloader.downloadPatch((size,totol) => print("$size/$totol"));
如何自定义下载URL
如果您觉得Shorebird的CDN访问仍然很慢或无法访问,您可以提前将补丁上传到自己的服务器上。
您可以自定义以下代码来实现它。
class ShorebirdCustomUrlDownloader extends ShorebirdDownloader {
ShorebirdUrlDownloader({required super.appid});
@override
Future<Patch?> requestPatchInfo() async {
// TODO: 发送HTTP请求以自定义您的服务器URL
// [number] 表示最新的补丁编号
// [downloadUrl] 表示最新的补丁下载地址
return Patch(number: number, downloadUrl: downloadUrl);
}
}
然后您可以像平常一样使用它。
final downloader =
ShorebirdCustomUrlDownloader(appid: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx');
await downloader.downloadPatch((size,totol) => print("$size/$totol"));
上传器
我们现在支持上传到Appwrite服务器
安装上传器
dart pub global activate shorebird_downloader
上传器到Appwrite服务器
首先,配置您的工厂文件
# pubspec.yaml
appwrite:
key: xxxxxx
projectId: xxxxx
bucketId: xxxx
# shorebird.yaml
app_id: xxxxx
您可以使用以下命令将最新的补丁上传到AppWrite。
# 在当前Shorebird项目中
shorebird_patch_uploader appwrite --platform [ios/android]
要直接从AppWrite下载更新,您需要使用以下代码
final downloader =
ShorebirdAppwriteDownloader(
appid: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
projectId: 'xxxxx',
bucketId: 'bucketId',
key: 'key',
// endPoint: '自定义AppWrite端点,正常情况下为 https://cloud.appwrite.io/v1'
);
await downloader.downloadPatch((size,totol) => print("$size/$totol"));
更多关于Flutter文件下载管理插件shorebird_downloader的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter文件下载管理插件shorebird_downloader的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
shorebird_downloader
是一个用于 Flutter 的文件下载管理插件,它可以帮助你方便地管理文件下载任务,包括开始、暂停、恢复和取消下载等操作。以下是如何使用 shorebird_downloader
插件的基本步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 shorebird_downloader
插件的依赖:
dependencies:
flutter:
sdk: flutter
shorebird_downloader: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来获取依赖。
2. 初始化插件
在你的 Dart 代码中,首先需要初始化 shorebird_downloader
插件:
import 'package:shorebird_downloader/shorebird_downloader.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await ShorebirdDownloader.initialize();
runApp(MyApp());
}
3. 开始下载
你可以通过 ShorebirdDownloader.download
方法来启动一个下载任务:
void startDownload() async {
String url = 'https://example.com/file.zip';
String savePath = '/path/to/save/file.zip';
String taskId = await ShorebirdDownloader.download(
url: url,
savePath: savePath,
onProgress: (progress) {
print('Download progress: $progress%');
},
onCompleted: () {
print('Download completed');
},
onError: (error) {
print('Download error: $error');
},
);
print('Download task started with ID: $taskId');
}
4. 暂停、恢复和取消下载
你可以通过任务 ID 来暂停、恢复或取消下载任务:
void pauseDownload(String taskId) async {
await ShorebirdDownloader.pause(taskId);
print('Download paused');
}
void resumeDownload(String taskId) async {
await ShorebirdDownloader.resume(taskId);
print('Download resumed');
}
void cancelDownload(String taskId) async {
await ShorebirdDownloader.cancel(taskId);
print('Download canceled');
}
5. 监听下载状态
你可以通过 ShorebirdDownloader.addListener
来监听下载任务的状态变化:
void addDownloadListener() {
ShorebirdDownloader.addListener((taskId, status) {
print('Task $taskId status: $status');
});
}
6. 获取下载任务列表
你可以通过 ShorebirdDownloader.getTasks
方法来获取当前所有的下载任务:
void getDownloadTasks() async {
List<DownloadTask> tasks = await ShorebirdDownloader.getTasks();
tasks.forEach((task) {
print('Task ID: ${task.taskId}, Status: ${task.status}, Progress: ${task.progress}%');
});
}
7. 处理下载完成后的文件
下载完成后,你可以通过 savePath
来访问下载的文件,并对其进行进一步处理。
8. 清理资源
在应用退出时,建议清理下载管理器的资源:
void disposeDownloader() async {
await ShorebirdDownloader.dispose();
}
完整示例
以下是一个完整的示例,展示了如何使用 shorebird_downloader
插件来下载文件并管理下载任务:
import 'package:flutter/material.dart';
import 'package:shorebird_downloader/shorebird_downloader.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await ShorebirdDownloader.initialize();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: DownloadPage(),
);
}
}
class DownloadPage extends StatefulWidget {
[@override](/user/override)
_DownloadPageState createState() => _DownloadPageState();
}
class _DownloadPageState extends State<DownloadPage> {
String _taskId;
void startDownload() async {
String url = 'https://example.com/file.zip';
String savePath = '/path/to/save/file.zip';
_taskId = await ShorebirdDownloader.download(
url: url,
savePath: savePath,
onProgress: (progress) {
print('Download progress: $progress%');
},
onCompleted: () {
print('Download completed');
},
onError: (error) {
print('Download error: $error');
},
);
print('Download task started with ID: $_taskId');
}
void pauseDownload() async {
if (_taskId != null) {
await ShorebirdDownloader.pause(_taskId);
print('Download paused');
}
}
void resumeDownload() async {
if (_taskId != null) {
await ShorebirdDownloader.resume(_taskId);
print('Download resumed');
}
}
void cancelDownload() async {
if (_taskId != null) {
await ShorebirdDownloader.cancel(_taskId);
print('Download canceled');
}
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Shorebird Downloader Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: startDownload,
child: Text('Start Download'),
),
ElevatedButton(
onPressed: pauseDownload,
child: Text('Pause Download'),
),
ElevatedButton(
onPressed: resumeDownload,
child: Text('Resume Download'),
),
ElevatedButton(
onPressed: cancelDownload,
child: Text('Cancel Download'),
),
],
),
),
);
}
}