提交 477b6d74 authored 作者: 张国庆's avatar 张国庆

feat:增加异常网络情况处理

上级 f8d0d395
...@@ -148,6 +148,19 @@ class ApkUpdatePlugin : FlutterPlugin, MethodCallHandler, EventChannel.StreamHan ...@@ -148,6 +148,19 @@ class ApkUpdatePlugin : FlutterPlugin, MethodCallHandler, EventChannel.StreamHan
openFile(path) openFile(path)
} }
override fun onFailed() {
mActivity?.runOnUiThread {
mEventSink?.success(
mapOf(
"currentSize" to 1,
"totalSize" to 1,
"isDownloadSuccess" to "0"
)
)
}
}
override fun onAttachedToActivity(binding: ActivityPluginBinding) { override fun onAttachedToActivity(binding: ActivityPluginBinding) {
mActivity = binding.activity mActivity = binding.activity
} }
......
...@@ -65,6 +65,7 @@ object DownloadAppUtil { ...@@ -65,6 +65,7 @@ object DownloadAppUtil {
clientException: ClientException?, clientException: ClientException?,
serviceException: ServiceException? serviceException: ServiceException?
) { ) {
progressCallback.onFailed()
// 请求异常。 // 请求异常。
clientException?.printStackTrace() clientException?.printStackTrace()
if (serviceException != null) { if (serviceException != null) {
......
...@@ -6,4 +6,6 @@ interface ProgressCallback { ...@@ -6,4 +6,6 @@ interface ProgressCallback {
fun success(path:String) fun success(path:String)
fun onFailed()
} }
\ No newline at end of file
import 'package:connectivity_plus/connectivity_plus.dart';
final Connectivity connectivity = Connectivity();
\ No newline at end of file
import 'dart:convert'; import 'dart:convert';
import 'package:apk_update/utils/connectivity_util.dart';
import 'package:apk_update/utils/toast_util.dart'; import 'package:apk_update/utils/toast_util.dart';
import 'package:apk_update/widget/update_dialog.dart'; import 'package:apk_update/widget/update_dialog.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:dio/dio.dart'; import 'package:dio/dio.dart';
import 'package:flustars_flutter3/flustars_flutter3.dart'; import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
...@@ -30,9 +32,17 @@ void checkVersion( ...@@ -30,9 +32,17 @@ void checkVersion(
Function()? jumpAppStore, // 跳转AppStore Function()? jumpAppStore, // 跳转AppStore
Function(String? path)? installApk, // 安装Apk Function(String? path)? installApk, // 安装Apk
Function()? downloadApkError, // 下载Apk错误 Function()? downloadApkError, // 下载Apk错误
Function(String? path,String? apkPath)? downloadApk, // 使用OSS下载Apk Function(String? path, String? apkPath)? downloadApk, // 使用OSS下载Apk
int? isOssDownload //1 使用OSS下载 int? isOssDownload //1 使用OSS下载
}) async { }) async {
// 判断网络是否连接
var resList = await connectivity.checkConnectivity();
if (resList.isNotEmpty && resList[0] == ConnectivityResult.none) {
ToastUtil.showToast("网络异常,请检查网络");
return;
}
try { try {
final Response response = await Dio().request( final Response response = await Dio().request(
url, url,
......
...@@ -18,7 +18,7 @@ class UpdateDialog extends StatefulWidget { ...@@ -18,7 +18,7 @@ class UpdateDialog extends StatefulWidget {
final Function()? jumpAppStore; // 跳转AppStore final Function()? jumpAppStore; // 跳转AppStore
final Function(String? path)? installApk; // 安装Apk final Function(String? path)? installApk; // 安装Apk
final int? isOssDownload; //1 使用OSS下载 final int? isOssDownload; //1 使用OSS下载
final Function(String? path,String? apkPath)? downloadApk; // 使用OSS下载Apk final Function(String? path, String? apkPath)? downloadApk; // 使用OSS下载Apk
final Function()? downloadApkError; // 下载Apk错误 final Function()? downloadApkError; // 下载Apk错误
const UpdateDialog({ const UpdateDialog({
...@@ -229,6 +229,14 @@ class _UpdateDialogState extends State<UpdateDialog> { ...@@ -229,6 +229,14 @@ class _UpdateDialogState extends State<UpdateDialog> {
var count = event['currentSize'] as num; var count = event['currentSize'] as num;
var total = event['totalSize'] as num; var total = event['totalSize'] as num;
var isDownloadSuccess = event['isDownloadSuccess'] == "1"; var isDownloadSuccess = event['isDownloadSuccess'] == "1";
var isDownloadFailed = event['isDownloadSuccess'] == "0";
if (isDownloadFailed) {
_isDownload = false;
setState(() {});
return;
}
_value = count / total; _value = count / total;
if (isDownloadSuccess) { if (isDownloadSuccess) {
_isDownload = false; _isDownload = false;
......
...@@ -23,6 +23,8 @@ dependencies: ...@@ -23,6 +23,8 @@ dependencies:
flutter_widget_from_html_core: ^0.10.0 flutter_widget_from_html_core: ^0.10.0
# https://pub.dev/packages/url_launcher # https://pub.dev/packages/url_launcher
url_launcher: ^6.1.2 url_launcher: ^6.1.2
#网络连接工具
connectivity_plus: ^6.1.0
dev_dependencies: dev_dependencies:
flutter_test: flutter_test:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论