I'm trying to login in users using a third party api. But the problem is whenever an error occurs and the catch error is executed the "then" function that holds the navigation to the HomeScreen also is executed. Please is there a way to login user only when there is no error.
void signIn() {
setState(() {
_isLoading = true;
});
final isValid = _formKey.currentState!.validate();
if (isValid == false) {
setState(() {
_isLoading = false;
});
return;
} else {
setState(() {
_isLoading = true;
});
Provider.of<Authorization>(context, listen: false)
.loginUser(
_emailController.text.trim(),
_passwordController.text.trim(),
)
.catchError((error) {
const snackBar = SnackBar(
backgroundColor: Colors.red,
content: Text("An error occured please try again."),
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}).then((_) {
setState(() {
_isLoading = false;
});
Navigator.push(
context,
MaterialPageRoute(
builder: ((context) => const HomeScreen()),
),
);
});
}
}
Thanks in advance.