1

enter image description here

This 3 functions are inside a class, the order in which I use them is the next:

  1. The login Query is called.

  2. If the password matches the username it will call function loginAccepted();

  3. This functions changes the value of a class variable to 1 meaning that the data that was entered is correct.

So far the code works they way. The problem comes now.

enter image description here

I have a ElevatedButton in which I:

  1. Call the loginQuery function from the other class.

  2. If the userMaster.loginVerification is 1 which would mean the data was correct, it will show a popup saying it was correct. (userMaster is the variable that im using to control the functions of that class.)

  3. In case loginVerification is not 1 it will also show a popup saying it.

enter image description here

So then, when i try my code entering a valid username and password it shows this which shows on the left, the console showing in the console the print confirming that the data is a valid user, but then on the right the program shows the popup as if the data I entered is not a valid user.

enter image description here

Then I type in the console userMaster.loginVerification to check whats happening and it shows a 1 which would mean the data I entered was valid but the popup got a 0 for that value which makes no sense.

I don't understand what is happening on my code, I am starting to believe that the functions are the last thing to get executed inside of a ElevatedButton or something, any help would be appreciated.

julemand101
  • 28,470
  • 5
  • 52
  • 48

2 Answers2

1

This is effectively this question but that is geared towards JS and you'll be confused. It's just async programming in general though.

  1. Make loginQuery return a Future
Future<void> loginQuery() async {
  var docs = await Firestore.instance.collection(...).where(...).getDocuments();
  // existing if statement
  if(docs.documents.isEmpty) {
   ...
  } else {
   ...
  }
}
  1. await the Future in your onPressed
onPressed:() async {
  await userMaster.loginQuery(...);
  // now do your if check
}
Adam Jenkins
  • 51,445
  • 11
  • 72
  • 100
0

Thats how the function in the class ended up:

Future<void> loginQuery(String user, String password) async {
Firestore.instance
    .collection('Usuarios')
    .where('usuario', isEqualTo: user)
    .getDocuments()
    .then((QuerySnapshot docs) {
  if (docs.documents.isNotEmpty) {
    var w = docs.documents[0].data;

    if (w['contraseña'] == password) {
      print('Datos verificados correctamente');
      loginAccepted();
    } else {
      print('El usuario no existe o la contraseña no corresponde');
      loginDeclined();
    }
  }
});}

Thats how the button ended up:

ElevatedButton(
        onPressed: () async {
          await userMaster.loginQuery(
              loginUserController.text, loginPasswordController.text);
          if (userMaster.loginVerification == 1) {
            showDialog(
                context: context,
                builder: (_) => AlertDialog(
                      title: Text('Inicio de sesion correcto.'),
                    ),
                barrierDismissible: true);
          } else {
            Navigator.pop(context);
            showDialog(
                context: context,
                builder: (_) => AlertDialog(
                      title: Text('Credenciales incorrectas.'),
                    ),
                barrierDismissible: true);
          }

          // Navigate back to first route when tapped.
        },
        child: Text('Ingresar'),
      ),
  • You **did not** do what I said and use `async/await` in `loginQuery`. You **need** to `var docs = await Firestore.instance().collection(...).where(...).getDocuments()` – Adam Jenkins Apr 26 '21 at 15:30
  • I didnt realize about the docs = await part, now I changed it and its working, thank you so much. – Martin Pizarro Apr 26 '21 at 15:47