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:
parent
d79b7cf500
commit
192bc476c4
|
|
@ -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<void> _startDownload() async {
|
||||
|
|
|
|||
Loading…
Reference in New Issue