fix(mobile): move _startDownload to didChangeDependencies to avoid initState context crash

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 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-03-07 09:57:38 -08:00
parent d79b7cf500
commit 192bc476c4
1 changed files with 8 additions and 1 deletions

View File

@ -246,19 +246,26 @@ class _DownloadProgressDialogState extends State<_DownloadProgressDialog> {
bool _hasError = false; bool _hasError = false;
bool _downloadCompleted = false; bool _downloadCompleted = false;
File? _downloadedApkFile; File? _downloadedApkFile;
// didChangeDependencies
bool _downloadStarted = false;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_startDownload(); // initState context.t() inherited widget
} }
@override @override
void didChangeDependencies() { void didChangeDependencies() {
super.didChangeDependencies(); super.didChangeDependencies();
// didChangeDependencies 使 context.t()
if (_statusText.isEmpty) { if (_statusText.isEmpty) {
_statusText = context.t('update.preparing'); _statusText = context.t('update.preparing');
} }
if (!_downloadStarted) {
_downloadStarted = true;
_startDownload();
}
} }
Future<void> _startDownload() async { Future<void> _startDownload() async {