void signUserIn() async {
showDialog(
context: context,
builder: (context) {
return const Center(
child: CircularProgressIndicator(),
);
},
);
try {
await FirebaseAuth.instance.signInWithEmailAndPassword(
email: emailController.text,
password: passwordController.text,
);
//pop the loading circle
Navigator.of(context).pop();
} on FirebaseException catch (e) {
//pop the loading circle
Navigator.of(context).pop();
if (e.code == 'user-not-found') {
wrongEmailMessage();
} else if (e.code == 'wrong-password') {
wrongPasswordMessage();
}
}
}
void wrongEmailMessage() {
showDialog(
context: context,
builder: (context) {
return const AlertDialog(
title: Text('Incorrect Email'),
);
},
);
}
void wrongPasswordMessage() {
showDialog(
context: context,
builder: (context) {
return const AlertDialog(
title: Text('Incorrect Password'),
);
},
);
}
Using Navigator.of(context).pop(); to remove the loading circle behaves as expected when clicking my sign in button as normal, but if I spam it, it leads to a black screen. Additionally, VS warns me to not use buildcontext across async methods. Is there a way to stop the double pop or improve my usage?
I tried using Navigator.of(context).pop(); which causes a black screen on spam. I want my loading circle to disappear on auth error, not a black screen.
2
Answers
No flutter você precisa fornecer o contexto correto do widget, pois sunpondo várias rotas com push, formará uma pilha. Sabendo disso, com a função você precisa especificar como paramêtro o contexto atual em que você está inserido. Exemplo
On the screen when you call function signUserIn, first you must be sure this is StatefullWidget, then you need to add a bool variable to state. Let’s name it _isLoading
Then you need to add this code to your function to handle multiple taps
In your layout, I reccomend you handle _isLoading variable to display CircularProgressIndicator
Now you don’t need to call the pop method to close the dialog with CircularProgressIndicator, so you can remove that code and it will be something like that
}
Well, it’s really simple and easy but I think it’s OK for a pet project but not for the production code.
In the best case, a button should call a state management and then just wait for another state to display. Please, read about state management, it really important part of Flutter applications.
flutter_bloc
riverpod