From 192bc476c4959d7023543b9425209de67fcd3f37 Mon Sep 17 00:00:00 2001 From: hailin Date: Sat, 7 Mar 2026 09:57:38 -0800 Subject: [PATCH] fix(mobile): move _startDownload to didChangeDependencies to avoid initState context crash MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit context.t() (Localizations) cannot be called during initState() — inherited widgets are not yet mounted. When download fails instantly (e.g. no network), setState with context.t() inside _startDownload fired synchronously within initState, causing: dependOnInheritedWidgetOfExactType called before initState() completed Fix: add _downloadStarted guard flag; call _startDownload() from didChangeDependencies() instead of initState(), where context is fully ready. Co-Authored-By: Claude Sonnet 4.6 --- .../lib/core/updater/channels/self_hosted_updater.dart | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/frontend/genex-mobile/lib/core/updater/channels/self_hosted_updater.dart b/frontend/genex-mobile/lib/core/updater/channels/self_hosted_updater.dart index fb27e1e..baad9ab 100644 --- a/frontend/genex-mobile/lib/core/updater/channels/self_hosted_updater.dart +++ b/frontend/genex-mobile/lib/core/updater/channels/self_hosted_updater.dart @@ -246,19 +246,26 @@ class _DownloadProgressDialogState extends State<_DownloadProgressDialog> { bool _hasError = false; bool _downloadCompleted = false; File? _downloadedApkFile; + // 防止 didChangeDependencies 多次触发时重复启动下载 + bool _downloadStarted = false; @override void initState() { super.initState(); - _startDownload(); + // 不在 initState 里调用 context.t(),需要等 inherited widget 挂载完成 } @override void didChangeDependencies() { super.didChangeDependencies(); + // didChangeDependencies 是首次可安全使用 context.t() 的时机 if (_statusText.isEmpty) { _statusText = context.t('update.preparing'); } + if (!_downloadStarted) { + _downloadStarted = true; + _startDownload(); + } } Future _startDownload() async {