From c104ec16ed64a3b4615f3ee64dcc4e28a5b832f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20M=C3=BCllner?= Date: Mon, 23 Feb 2026 09:01:15 +0100 Subject: [PATCH] fix: show success snackbar from HomeScreen instead of dying dialog The AddCarDialog now pops with a boolean result instead of trying to show a SnackBar on its own context after Navigator.pop() disposes it. HomeScreen shows the snackbar using its own stable mounted context. --- lib/main.dart | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 2a28b2e..5b676aa 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -411,12 +411,23 @@ class _HomeScreenState extends State { if (!mounted) return; setState(() => _isBusy = false); - await showDialog( - context: context, - builder: (_) => data != null - ? _AlreadyExistsDialog(hwId: hwId) - : _AddCarDialog(hwId: hwId), - ); + if (data != null) { + await showDialog( + context: context, + builder: (_) => _AlreadyExistsDialog(hwId: hwId), + ); + } else { + // Dialog returns true if the car was added successfully. + final added = await showDialog( + context: context, + builder: (_) => _AddCarDialog(hwId: hwId), + ); + if (added == true && mounted) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text('$hwId added to your collection! 🎉')), + ); + } + } } catch (e) { if (!mounted) return; setState(() => _isBusy = false); @@ -469,10 +480,8 @@ class _AddCarDialogState extends State<_AddCarDialog> { 'user_id': supabase.auth.currentUser!.id, }); if (!mounted) return; - Navigator.of(context).pop(); - ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text('${widget.hwId} added to your collection! 🎉')), - ); + // Return true to signal success — snackbar shown by HomeScreen. + Navigator.of(context).pop(true); } catch (e) { if (!mounted) return; setState(() => _isAdding = false);