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
Instead of:
Try:
Here’s a corrected version of your mainAccess function: