skip to Main Content

I am starting using Flutter and Firebase to make small app.

I am using the Email and Password feature from firebase and i got this module to know if a document with the same name as the user UID exists on firebase.

  Future<bool> documentTiendaNExist(String id) async {
    DocumentSnapshot<Map<String, dynamic>> document = await FirebaseFirestore
        .instance
        .collection("DocumentName")
        .doc(id)
        .get();

    if (document.exists) {
      return true;
    } else {
      return false;
    }
  }

And this is the code where want to log in and send me to the main page if the document exist or to the Registration page if the document doesn’t exists.

mainAccess() async {
    if (passwordcontroller.text != "" && mailcontroller.text != "") {
        await FirebaseAuth.instance
            .signInWithEmailAndPassword(email: email, password: password);


   if (await DatabaseMethods().documentTiendaNExist(id) == true)
   {
   //OPEN MAIN PAGE
   }else{
   //OPEN REGISTRATION PAGE
   }
}

The problem is that when i log in and i have the document on the database, the value of documentTiendaNExist stays on False and sends me to the Registration page instead of the main page

What i am seeing on the breakpoint and the output is that the documentTiendaNExist value changes to True after opening the main page, meaning that is taking a lot of time to change the value or maybe i am calling it in a wrong way.

Any help or tip will be appreaciated

2

Answers


  1. Instead of:

       if (await DatabaseMethods().documentTiendaNExist(id) == true)
       {
       //OPEN MAIN PAGE
       }else{
       //OPEN REGISTRATION PAGE
       }
    

    Try:

    await DatabaseMethods().documentTiendaNExist(id).then((bool documentExist) {
            if (documentExist) {
              //OPEN MAIN PAGE
            } else {
              //OPEN REGISTRATION PAGE
            }
          });
    
    Login or Signup to reply.
  2. Here’s a corrected version of your mainAccess function:

     mainAccess() async {
          if (passwordcontroller.text.isNotEmpty && mailcontroller.text.isNotEmpty) {
            try {
              UserCredential userCredential = await FirebaseAuth.instance
                  .signInWithEmailAndPassword(
                      email: mailcontroller.text, 
                      password: passwordcontroller.text);
        
              String uid = userCredential.user!.uid;
        
              bool documentExists = await DatabaseMethods().documentTiendaNExist(uid);
        
              if (documentExists) {
                Navigator.pushReplacementNamed(context, '/mainPage');
              } else {
                Navigator.pushReplacementNamed(context, '/registrationPage');
              }
            } catch (e) {
              print("Error: $e");
            }
          } else {
            print("Please enter both email and password.");
          }
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search