From 1e1c7d9552fb5f6bf228d1f72078e5d2a2f72436 Mon Sep 17 00:00:00 2001 From: Marc Rejohn Castillano Date: Mon, 9 Mar 2026 18:24:44 +0800 Subject: [PATCH] Fixed freezing on start up for some devices --- lib/main.dart | 67 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 12 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index eb9ac748..e5d2ad1f 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -240,10 +240,20 @@ Future main() async { pdfrxFlutterInitialize(dismissPdfiumWasmWarnings: true); // initialize Firebase before anything that uses messaging - await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform); + try { + await Firebase.initializeApp( + options: DefaultFirebaseOptions.currentPlatform, + ).timeout(const Duration(seconds: 15)); + } catch (e) { + debugPrint('Firebase init failed or timed out: $e'); + } FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler); - await dotenv.load(fileName: '.env'); + try { + await dotenv.load(fileName: '.env').timeout(const Duration(seconds: 5)); + } catch (e) { + debugPrint('dotenv load failed or timed out: $e'); + } AppTime.initialize(location: 'Asia/Manila'); @@ -255,11 +265,26 @@ Future main() async { return; } - await Supabase.initialize(url: supabaseUrl, anonKey: supabaseAnonKey); + try { + await Supabase.initialize( + url: supabaseUrl, + anonKey: supabaseAnonKey, + ).timeout(const Duration(seconds: 20)); + } catch (e) { + debugPrint('Supabase init failed or timed out: $e'); + runApp(const _MissingConfigApp()); + return; + } // Initialize background location service (Workmanager) if (!kIsWeb) { - await initBackgroundLocationService(); + try { + await initBackgroundLocationService().timeout( + const Duration(seconds: 10), + ); + } catch (e) { + debugPrint('Background location service init failed or timed out: $e'); + } } // ensure token saved shortly after startup if already signed in. @@ -312,20 +337,38 @@ Future main() async { // on Android 13+ we must request POST_NOTIFICATIONS at runtime; without it // notifications are automatically denied and cannot be re‑enabled from the // system settings. The helper uses `permission_handler`. - final granted = await ensureNotificationPermission(); - if (!granted) { - // we don’t block startup, but it’s worth logging so developers notice. - // debugPrint('notification permission not granted'); + try { + final granted = await ensureNotificationPermission().timeout( + const Duration(seconds: 10), + ); + if (!granted) { + // we don't block startup, but it's worth logging so developers notice. + // debugPrint('notification permission not granted'); + } + } catch (e) { + debugPrint('Notification permission request failed or timed out: $e'); } // Request location permission at launch (same pattern as notification) - final locationGranted = await ensureLocationPermission(); - if (!locationGranted) { - // debugPrint('location permission not granted'); + try { + final locationGranted = await ensureLocationPermission().timeout( + const Duration(seconds: 10), + ); + if (!locationGranted) { + // debugPrint('location permission not granted'); + } + } catch (e) { + debugPrint('Location permission request failed or timed out: $e'); } // request FCM permission (iOS/Android13+) and handle foreground messages - await FirebaseMessaging.instance.requestPermission(); + try { + await FirebaseMessaging.instance.requestPermission().timeout( + const Duration(seconds: 10), + ); + } catch (e) { + debugPrint('FCM permission request failed or timed out: $e'); + } } FirebaseMessaging.onMessage.listen((RemoteMessage message) async {