CW-695: Fix Zone Mismatch Error Triggered By Restoring Backup (#1651)

* Fix: Zone mismatch error triggered by restoring backup * fix: Adjust check for reinitializing

David Adegoke committed Sep 5, 2024 at 03:59 UTC 3869a71bd1306e6b8e2f7c06c44d01e7c0a48c3f
2 files changed +51 -32
lib/main.dart
+50 -31
@@ -48,11 +48,14 @@ final rootKey = GlobalKey<RootState>();
48 final RouteObserver<PageRoute<dynamic>> routeObserver = RouteObserver<PageRoute<dynamic>>();
49
50 Future<void> main() async {
51 + await runAppWithZone();
52 +}
53
54 +Future<void> runAppWithZone() async {
55 bool isAppRunning = false;
56 +
57 await runZonedGuarded(() async {
58 WidgetsFlutterBinding.ensureInitialized();
55 -
59 FlutterError.onError = ExceptionHandler.onError;
60
61 /// A callback that is invoked when an unhandled error occurs in the root
@@ -62,42 +65,14 @@ Future<void> main() async {
65
66 return true;
67 };
65 -
66 - await setDefaultMinimumWindowSize();
67 -
68 - await CakeHive.close();
69 -
70 - await initializeAppConfigs();
68 + await initializeAppAtRoot();
69
70 runApp(App());
73 -
71 isAppRunning = true;
72 }, (error, stackTrace) async {
73 if (!isAppRunning) {
74 runApp(
78 - MaterialApp(
79 - debugShowCheckedModeBanner: false,
80 - scrollBehavior: AppScrollBehavior(),
81 - home: Scaffold(
82 - body: SingleChildScrollView(
83 - child: Container(
84 - margin: EdgeInsets.only(top: 50, left: 20, right: 20, bottom: 20),
85 - child: Column(
86 - children: [
87 - Text(
88 - 'Error:\n${error.toString()}',
89 - style: TextStyle(fontSize: 22),
90 - ),
91 - Text(
92 - 'Stack trace:\n${stackTrace.toString()}',
93 - style: TextStyle(fontSize: 16),
94 - ),
95 - ],
96 - ),
97 - ),
98 - ),
99 - ),
100 - ),
75 + TopLevelErrorWidget(error: error, stackTrace: stackTrace),
76 );
77 }
78
@@ -105,6 +80,12 @@ Future<void> main() async {
80 });
81 }
82
83 +Future<void> initializeAppAtRoot({bool reInitializing = false}) async {
84 + if (!reInitializing) await setDefaultMinimumWindowSize();
85 + await CakeHive.close();
86 + await initializeAppConfigs();
87 +}
88 +
89 Future<void> initializeAppConfigs() async {
90 setRootDirFromEnv();
91 final appDir = await getAppDir();
@@ -338,3 +319,41 @@ class _HomeState extends State<_Home> {
319 return const SizedBox.shrink();
320 }
321 }
322 +
323 +class TopLevelErrorWidget extends StatelessWidget {
324 + const TopLevelErrorWidget({
325 + required this.error,
326 + required this.stackTrace,
327 + super.key,
328 + });
329 +
330 + final Object error;
331 + final StackTrace stackTrace;
332 +
333 + @override
334 + Widget build(BuildContext context) {
335 + return MaterialApp(
336 + debugShowCheckedModeBanner: false,
337 + scrollBehavior: AppScrollBehavior(),
338 + home: Scaffold(
339 + body: SingleChildScrollView(
340 + child: Container(
341 + margin: EdgeInsets.only(top: 50, left: 20, right: 20, bottom: 20),
342 + child: Column(
343 + children: [
344 + Text(
345 + 'Error:\n${error.toString()}',
346 + style: TextStyle(fontSize: 22),
347 + ),
348 + Text(
349 + 'Stack trace:\n${stackTrace.toString()}',
350 + style: TextStyle(fontSize: 16),
351 + ),
352 + ],
353 + ),
354 + ),
355 + ),
356 + ),
357 + );
358 + }
359 +}
lib/view_model/restore_from_backup_view_model.dart
+1 -1
@@ -46,7 +46,7 @@ abstract class RestoreFromBackupViewModelBase with Store {
46 final data = await file.readAsBytes();
47
48 await backupService.importBackup(data, password);
49 - await main();
49 + await initializeAppAtRoot(reInitializing: true);
50
51 final store = getIt.get<AppStore>();
52 ReactionDisposer? reaction;