Once i press the rounded button, and if there is any error, i want flutter to show me the error in an alert window on my screen but it's not doing so.
RoundedButton(
title: 'Register',
onPressed: () async {
setState(() {
showSpinner = true;
});
try {
final newUser = await _auth.createUserWithEmailAndPassword(
email: email,
password: password,
);
Navigator.pushNamed(context, LoginScreen.id);
setState(() {
showSpinner = false;
});
} catch (e) {
build(context);
setState(() {
showSpinner = false;
});
AlertWindow(
error: e.toString(), // Here is the Alert window class
);
}
},
color: Colors.blueAccent,
),
class AlertWindow extends StatelessWidget {
const AlertWindow({Key? key, required this.error}) : super(key: key);
final String error;
@override
Widget build(BuildContext context) {
return BackdropFilter(
filter: ImageFilter.blur(sigmaX: 6, sigmaY: 6),
child: AlertDialog(
title: const Text('Alert'),
content: Text(error),
),
);
}
}
If any error comes from firebase the spinner stops Spinning and nothing more happens. The alert window does not Pop up.
CodePudding user response:
You should call showDialog function.
Sample:
// show the dialog
showDialog(
context: context,
builder: (BuildContext context) {
return AlertWindow(error: e.toString());
},
);
}
CodePudding user response:
Call the showDialog
method and pass the AlertWindow
like so:
try {
final newUser = await _auth.createUserWithEmailAndPassword(
email: email,
password: password,
);
Navigator.pushNamed(context, LoginScreen.id);
setState(() {
showSpinner = false;
});
} catch (e) {
build(context);
setState(() {
showSpinner = false;
});
showDialog(
context: context,
builder: (_) {
return Dialog(child: AlertWindow(error: e.toString()));
},
);
}