From bbcacc0664b91619aad12051059175165b93f00f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20M=C3=BCllner?= Date: Mon, 23 Feb 2026 08:50:39 +0100 Subject: [PATCH] fix: only rebuild AuthGate on actual login state changes - AuthGate now tracks wasLoggedIn vs isLoggedIn and only calls setState when the login status actually flips, preventing token-refresh events from tearing down open dialogs and causing _dependents.isEmpty crashes - Fix TextEditingController disposal order in scanner manual entry dialog --- lib/main.dart | 20 +++++++++++++------- lib/scanner_screen.dart | 5 +++-- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 7008cb5..76d3bea 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -59,18 +59,24 @@ class _AuthGateState extends State { // 1. Check for an existing session on cold start. _session = supabase.auth.currentSession; - // 2. Listen for auth state changes (sign-in, sign-out, - // password-recovery deep link, token refresh, etc.) + // 2. Listen for auth state changes — but only rebuild when login + // status actually changes (signed-in ↔ signed-out), NOT on + // every token refresh, to avoid tearing down open dialogs. supabase.auth.onAuthStateChange.listen( (AuthState authState) { if (!mounted) return; - setState(() { - _session = authState.session; - }); + + final wasLoggedIn = _session != null; + final isLoggedIn = authState.session != null; + _session = authState.session; + + // Only rebuild the tree when login state actually flips. + if (wasLoggedIn != isLoggedIn) { + setState(() {}); + } // If the user just clicked a password-reset link from their email, - // Supabase fires a PASSWORD_RECOVERY event. We can navigate them - // to a "set new password" screen here. + // Supabase fires a PASSWORD_RECOVERY event. if (authState.event == AuthChangeEvent.passwordRecovery) { _showResetPasswordDialog(); } diff --git a/lib/scanner_screen.dart b/lib/scanner_screen.dart index 537ed11..9ff5d3d 100644 --- a/lib/scanner_screen.dart +++ b/lib/scanner_screen.dart @@ -243,10 +243,11 @@ class _ScannerScreenState extends State { ), ); - controller.dispose(); - if (result != null && context.mounted) { + controller.dispose(); Navigator.of(context).pop(result); + } else { + controller.dispose(); } } }