I have a process that I want to run in a specific order when registering a new user.
- The new user clicks on the Create Account link
- The new user is taken to the User Registration screen where they can enter name, email, and password
- The click on the Register button.
- They are taken to a subscription payment page where they click on the Subscribe Now link. This page is part of the Stripe payment kit.
- The Stripe payment screen is shown so they can enter in payment information and the user
This is in the backend
6. Payment is made using Stripe
7. The user is registered into the Firebase authentication
8. A verification email is sent to the user
However, between steps 4 and 5, the verification email is sent. I think this is because of the “await” statement on the Subcribe Now button.
How do I get the execution to wait until the payment method has completed?
Here is the code for the button:
TextButton(
child: const Text('Subscribe Now!'),
onPressed: () async {
await makePayment(); <<<< I WANT EXECUTION TO WAIT HERE
/// Register the new user
try {
final newUser = await _auth.createUserWithEmailAndPassword(
email: widget.email, password: widget.password);
if (newUser != null) {
ref
.read(usersNotifierProvider.notifier)
.updateuserID(newUser.user!.uid);
ref
.read(usersNotifierProvider.notifier)
.updateEmail(newUser.user!.email!);
ref
.read(globalsNotifierProvider.notifier)
.updatenewUser(true);
ref
.read(globalsNotifierProvider.notifier)
.updatenewCompany(true);
/// Send the email verification
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (context) => const VerifyEmailScreen()));
} else {
setState(() {
registrationFail = true;
});
}
setState(() {
showSpinner = false;
});
} on FirebaseAuthException catch (error) {
switch (error.code) {
case "ERROR_INVALID_EMAIL":
case "invalid-email":
errorMessage =
"Your email address appears to be malformed.";
break;
case "email-already-in-use":
errorMessage = "Email is already in use.";
break;
case "ERROR_WRONG_PASSWORD":
case "wrong-password":
errorMessage = "Your password is wrong.";
break;
case "weak-password":
errorMessage = "Weak password";
break;
case "ERROR_USER_NOT_FOUND":
case "user-not-found":
errorMessage = "User with this email doesn't exist.";
break;
case "ERROR_USER_DISABLED":
case "user-disabled":
errorMessage =
"User with this email has been disabled.";
break;
case "ERROR_TOO_MANY_REQUESTS":
case "too-many-requests":
errorMessage = "Too many requests. Try again later.";
break;
case "ERROR_OPERATION_NOT_ALLOWED":
case "operation-not-allowed":
errorMessage =
"Signing in with Email and Password is not enabled.";
break;
default:
errorMessage =
"An undefined Error happened. Please try again.";
}
if (errorMessage != "") {
ScaffoldMessenger.of(context).showSnackBar((SnackBar(
content: Center(
child: Text(
errorMessage,
style: const TextStyle(
color: Colors.black,
fontWeight: FontWeight.w900),
),
),
behavior: SnackBarBehavior.floating,
margin: EdgeInsets.only(
bottom: MediaQuery.of(context).size.height - 100,
left: 10,
right: 10,
),
backgroundColor: Colors.redAccent,
)));
}
}
}),
Thanks for your help!