From bc22e9a24de8686a581b838935ab8832de52f4b6 Mon Sep 17 00:00:00 2001 From: Ivan Wong Date: Mon, 13 Jul 2026 18:16:20 +0800 Subject: [PATCH] fix: resolve import backup silently failing due to deactivated widget context The _import() function was called with a context that becomes deactivated when the confirmation dialog is popped, causing context.read() to throw an unsafe ancestor lookup error. The error was silently swallowed because _import() was not awaited. Fix: read service providers before showing the dialog while the context is still valid, then call importBackup directly without relying on the widget context. --- lib/widgets/settings/backup_restore_page.dart | 48 ++++--------------- 1 file changed, 8 insertions(+), 40 deletions(-) diff --git a/lib/widgets/settings/backup_restore_page.dart b/lib/widgets/settings/backup_restore_page.dart index 52b3df10..45cb1a7d 100644 --- a/lib/widgets/settings/backup_restore_page.dart +++ b/lib/widgets/settings/backup_restore_page.dart @@ -207,6 +207,9 @@ class BackupRestorePage extends StatelessWidget { Future _confirmFileImport( BuildContext context, AppLocalizations localizations, BackupFileEntry entry) async { + final backupService = context.read(); + final settingsService = context.read(); + final appsService = context.read(); showDialog( context: context, builder: (dialogContext) => AlertDialog( @@ -220,7 +223,11 @@ class BackupRestorePage extends StatelessWidget { TextButton( onPressed: () async { Navigator.of(dialogContext).pop(); - _import(context, localizations, entry.file); + try { + await backupService.importBackup(entry.file); + settingsService.reload(); + await appsService.refreshState(); + } catch (_) {} }, child: const Text("Import"), ), @@ -228,43 +235,4 @@ class BackupRestorePage extends StatelessWidget { ), ); } - - Future _import(BuildContext context, AppLocalizations localizations, File file) async { - try { - await context.read().importBackup(file); - if (context.mounted) { - context.read().reload(); - await context.read().refreshState(); - showDialog( - context: context, - builder: (context) => AlertDialog( - title: const Text("Import Success"), - content: Text(localizations.importSuccess), - actions: [ - TextButton( - onPressed: () => Navigator.of(context).pop(), - child: const Text("OK"), - ), - ], - ), - ); - } - } catch (e) { - if (context.mounted) { - showDialog( - context: context, - builder: (context) => AlertDialog( - title: const Text("Import Failed"), - content: Text(localizations.importError(e.toString())), - actions: [ - TextButton( - onPressed: () => Navigator.of(context).pop(), - child: const Text("OK"), - ), - ], - ), - ); - } - } - } }