dimanche 1 mars 2020

Flutter firebase email/password error handling doesn't check blank fields properly

Hello Flutter newbie here! I have setup the login and error checking part.

I want to do some checking on the fields, but I realize if the user does the following behavior, my code for error checking won't work:

  1. enter some text in email textfield
  2. enter some text in password textfield
  3. backspace delete the text in email textfield
  4. hit the flat button to submit

the code will pass the if checking and go ahead to sign in... but I thought since the email textfield is empty by the time I hit the button, it should still follow the if checking?

thanks in advance!

            FlatButton(
              onPressed: () async {
                if (password == null && email == null) {
                  print('empty sign in info');
                  setState(() {
                    alertText = 'You have not entered your login info!';
                  });
                } else if (password == null) {
                  setState(() {
                    alertText = 'Password cannot be blank!';
                  });
                } else if (email == null) {
                  setState(() {
                    alertText = 'Email cannot be blank!';
                  });
                } else {
                  setState(() {
                    showSpinner = true;
                  });
                  try {
                    final newUser = await _auth.signInWithEmailAndPassword(
                        email: email, password: password);
                    if (newUser != null) {
                      Navigator.pushNamed(context, ViewScreen.id);
                    }
                  } catch (e) {
                    print(e);
                  }
                }
              },

Aucun commentaire:

Enregistrer un commentaire