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.
This commit is contained in:
Lukas Müllner 2026-02-23 09:01:15 +01:00
parent 16d1145efd
commit c104ec16ed

View file

@ -411,12 +411,23 @@ class _HomeScreenState extends State<HomeScreen> {
if (!mounted) return;
setState(() => _isBusy = false);
if (data != null) {
await showDialog<void>(
context: context,
builder: (_) => data != null
? _AlreadyExistsDialog(hwId: hwId)
: _AddCarDialog(hwId: hwId),
builder: (_) => _AlreadyExistsDialog(hwId: hwId),
);
} else {
// Dialog returns true if the car was added successfully.
final added = await showDialog<bool>(
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);