I have implemented the below code. The problem is that I need to know whether the user account was created or not in order to forward the user to the homescreen. But since I am returning a Fluttertoast (to show the user an error message), I cannot return null. So I want to check whether the return value is a Fluttertoast. How can I do that? Or is there a better way to implement this?
Future registerWithEmailAndPassword(
{required String email,
required String password,
required String userName,
required String gender}) async {
try {
UserCredential result = await _auth.createUserWithEmailAndPassword(
email: email, password: password);
User? user = result.user;
// create a new document for the user with the uid
await DatabaseService(uid: user!.uid)
.updateUserData(gender: gender, userMail: email, userName: userName);
return user;
} catch (error) {
return Fluttertoast.showToast(
msg: error.toString(),
gravity: ToastGravity.TOP,
backgroundColor: Colors.black,
textColor: Colors.white);
}
}
Basically I then want to implement an if statement to check whether the user account was created or not (then the fluttertoast is returned). Something like:
if (result == FToast()) {
print("user not created");
}
CodePudding user response:
Try returning null
instead of Fluttertoast
.
Change you code as
Future registerWithEmailAndPassword(
{required String email,
required String password,
required String userName,
required String gender}) async {
try {
UserCredential result = await _auth.createUserWithEmailAndPassword(
email: email, password: password);
User? user = result.user;
// create a new document for the user with the uid
await DatabaseService(uid: user!.uid)
.updateUserData(gender: gender, userMail: email, userName: userName);
return user;
} catch (error) {
Fluttertoast.showToast(
msg: error.toString(),
gravity: ToastGravity.TOP,
backgroundColor: Colors.black,
textColor: Colors.white);
return null;
}
Show the toast here and return the null.
So you can see the error in toast and late you can check whether the user created by the user value null or not.
if (result == null) {
print("user not created");
}