Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter Firebase: How to check if return value is a specific object

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 if (result == FToast()) {
                              print("user not created");
                            }

>Solution :

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");
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading