From bde7f0c53b4e240330e1a2b39d5ad715bd9dd8bb Mon Sep 17 00:00:00 2001 From: hailin Date: Fri, 30 Jan 2026 12:20:56 -0800 Subject: [PATCH] =?UTF-8?q?fix(mining-app):=20=E8=87=AA=E5=8A=A8=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E6=A3=80=E6=9F=A5=E6=9C=AA=E8=A7=A6=E5=8F=91=20?= =?UTF-8?q?=E2=80=94=20=E4=BF=AE=E5=A4=8D=E5=8F=8C=E9=87=8D=E8=8A=82?= =?UTF-8?q?=E6=B5=81=20+=20=E6=B7=BB=E5=8A=A0=E8=B0=83=E8=AF=95=E6=97=A5?= =?UTF-8?q?=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题原因: 1. checkForUpdate() 未传 force:true,被 UpdateService 内部的60分钟 SharedPreferences 间隔二次节流,导致实际请求被跳过 2. 缺少日志输出,无法诊断检查流程是否执行 修复: - checkForUpdate(force: true) 跳过内部60分钟限制, 因为 MainShell 已有自己的90-300秒随机冷却节流 - 在 initState / dispose / didChangeAppLifecycleState / _checkForUpdateIfNeeded 各关键节点添加 debugPrint 日志, 方便 flutter run 控制台追踪完整检查流程 Co-Authored-By: Claude Opus 4.5 --- .../lib/presentation/widgets/main_shell.dart | 54 ++++++++++++------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/frontend/mining-app/lib/presentation/widgets/main_shell.dart b/frontend/mining-app/lib/presentation/widgets/main_shell.dart index 8a6bd8a2..defd626f 100644 --- a/frontend/mining-app/lib/presentation/widgets/main_shell.dart +++ b/frontend/mining-app/lib/presentation/widgets/main_shell.dart @@ -22,20 +22,24 @@ class _MainShellState extends State with WidgetsBindingObserver { @override void initState() { super.initState(); + debugPrint('[MainShell] initState called, adding WidgetsBindingObserver'); WidgetsBinding.instance.addObserver(this); WidgetsBinding.instance.addPostFrameCallback((_) { + debugPrint('[MainShell] postFrameCallback → 触发首次更新检查'); _checkForUpdateIfNeeded(); }); } @override void dispose() { + debugPrint('[MainShell] dispose called, removing WidgetsBindingObserver'); WidgetsBinding.instance.removeObserver(this); super.dispose(); } @override void didChangeAppLifecycleState(AppLifecycleState state) { + debugPrint('[MainShell] didChangeAppLifecycleState: $state'); if (state == AppLifecycleState.resumed) { _checkForUpdateIfNeeded(); } @@ -45,29 +49,41 @@ class _MainShellState extends State with WidgetsBindingObserver { final now = DateTime.now(); // 如果从未检查过,或者已过冷却时间,则检查 - if (_nextCheckAllowedTime == null || now.isAfter(_nextCheckAllowedTime!)) { - // 设置下次允许检查时间(90-300秒随机间隔) - final randomSeconds = 90 + Random().nextInt(211); - _nextCheckAllowedTime = now.add(Duration(seconds: randomSeconds)); + if (_nextCheckAllowedTime != null && now.isBefore(_nextCheckAllowedTime!)) { + debugPrint('[MainShell] 跳过更新检查,冷却中 (下次: $_nextCheckAllowedTime)'); + return; + } - // 延迟3秒,避免启动时干扰用户 - await Future.delayed(const Duration(seconds: 3)); + // 设置下次允许检查时间(90-300秒随机间隔) + final randomSeconds = 90 + Random().nextInt(211); + _nextCheckAllowedTime = now.add(Duration(seconds: randomSeconds)); + debugPrint('[MainShell] 准备检查更新,延迟3秒...'); + + // 延迟3秒,避免启动时干扰用户 + await Future.delayed(const Duration(seconds: 3)); + if (!mounted) { + debugPrint('[MainShell] widget 已 unmount,取消检查'); + return; + } + + try { + debugPrint('[MainShell] 开始调用 UpdateService.checkForUpdate(force: true)'); + // force: true 跳过 UpdateService 内部的60分钟间隔限制 + // 因为我们已有自己的90-300秒冷却节流 + final result = await UpdateService.instance.checkForUpdate(force: true); + debugPrint('[MainShell] 检查结果: hasUpdate=${result.hasUpdate}, error=${result.error}'); if (!mounted) return; - try { - final result = await UpdateService.instance.checkForUpdate(); - if (!mounted) return; - - if (result.hasUpdate && result.versionInfo != null) { - await SelfHostedUpdater.show( - context, - versionInfo: result.versionInfo!, - isForceUpdate: result.versionInfo!.isForceUpdate, - ); - } - } catch (e) { - debugPrint('[MainShell] 检查更新失败: $e'); + if (result.hasUpdate && result.versionInfo != null) { + debugPrint('[MainShell] 发现新版本: ${result.versionInfo!.versionName},弹出更新对话框'); + await SelfHostedUpdater.show( + context, + versionInfo: result.versionInfo!, + isForceUpdate: result.versionInfo!.isForceUpdate, + ); } + } catch (e) { + debugPrint('[MainShell] 检查更新失败: $e'); } }