简体   繁体   中英

Firebase Message to display invalid email/error when trying to sign in

I am very new to flutter, I already connect the Firebase function with sign in button. It work perfectly, the only thing I wanted to do is to add Error message if the email and password is not found, not registered etc, for example there is no known id inside the firebase of the user account. It will show 'User not found", invalid email or password etc.

Container(
                padding: const EdgeInsets.symmetric(horizontal: 40),
                child: Column(
                  children: [
                    Padding(
                      padding: const EdgeInsets.symmetric(vertical: 15.0),
                      child: Container(
                        decoration: BoxDecoration(
                            color: Colors.grey[500].withOpacity(0.5),
                            borderRadius: BorderRadius.circular(16)),
                        child: TextFormField(
                          onChanged: (value) {
                            setState(() {
                              _email = value.trim();
                            });
                          },
                          decoration: InputDecoration(
                            contentPadding:
                                const EdgeInsets.symmetric(vertical: 20.0),
                            border: InputBorder.none,
                            hintText: 'Email',
                            hintStyle: TextStyle(
                                fontSize: 20.0, color: Colors.white),
                            prefixIcon: Padding(
                              padding: const EdgeInsets.symmetric(
                                  horizontal: 20.0),
                              child: Icon(
                                FontAwesomeIcons.solidEnvelope,
                                color: Colors.white,
                                size: 20.0,
                              ),
                            ),
                          ),
                          style: TextStyle(
                              fontSize: 20.0, color: Colors.white),
                          keyboardType: TextInputType.emailAddress,
                          textInputAction: TextInputAction.next,
                        ),
                      ),
                    ),
                    Container(
                      decoration: BoxDecoration(
                          color: Colors.grey[500].withOpacity(0.5),
                          borderRadius: BorderRadius.circular(16)),
                      child: TextFormField(
                        onChanged: (value) {
                          setState(() {
                            _password = value.trim();
                          });
                        },
                        decoration: InputDecoration(
                            contentPadding:
                                const EdgeInsets.symmetric(vertical: 20.0),
                            border: InputBorder.none,
                            hintText: 'Password',
                            hintStyle: TextStyle(
                                fontSize: 20.0, color: Colors.white),
                            prefixIcon: Padding(
                              padding: const EdgeInsets.symmetric(
                                  horizontal: 20.0),
                              child: Icon(
                                FontAwesomeIcons.lock,
                                color: Colors.white,
                                size: 20.0,
                              ),
                            ),
                            suffixIcon: Icon(
                              FontAwesomeIcons.eye,
                              color: Colors.white,
                              size: 20.0,
                            )),
                        obscureText: true,
                        style:
                            TextStyle(fontSize: 20.0, color: Colors.white),
                        keyboardType: TextInputType.name,
                        textInputAction: TextInputAction.done,
                      ),
                    ),
                    SizedBox(
                      height: 20.0,
                    ),
                    RoundedButton(
                        text: "Login",
                        color: Colors.grey[600],
                        press: () async {
                          await auth.signInWithEmailAndPassword(
                              email: _email, password: _password);
                          Navigator.of(context).pushReplacement(
                              MaterialPageRoute(
                                  builder: (context) => WelcomeScreen()));
                        }),

Any help is appreciated. Thank you. im very new to stack overflow too. sorry if my question format wrong

You should always surround your sign in code with a try/catch statement:

try {
    await auth.signInWithEmailAndPassword(
       email: _email, password: _password);
} on FirebaseAuthException catch (e) {
    //here you get the error messages and do stuff accordingly
    if(e.code == 'wrong-password') {
        //handle wrong password like in an alertdialog
    }
}

There are different codes that will be returned when an error occurs:

  • 'user-not-found'
  • 'invalid-email'
  • 'user-disabled'
  • 'operation-not-allowed'

And so on. You can check out the full error code list on this site:

Firebase erorrs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM