skip to Main Content

I’m new to flutter .So I was wondering if there is anyway that I can show the firebase auth error codes as the Errortext in textfield ?
enter image description here

I am able to print the exception from firebase_auth but dont know how to give it as the errortext .

 try {
                  final newuser = await FirebaseAuth.instance
                      .createUserWithEmailAndPassword(
                    email: email ?? 'error',
                    password: password ?? 'error',
                  );

                  if (newuser != null) {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => ProfileDetail()),
                    );
                  }
                } catch (e) {
                  print(e);
                  
                }
              },

this is the textfield

   TextField(
                      onChanged: (value) {
                        username = value;
                      },
                      decoration: const InputDecoration(
                        border: UnderlineInputBorder(),
                        labelText: 'Username',
                        // errorText: 
                        
                      ),
                    ),

3

Answers


  1. You can use "showsnackBar"

    try {
        showDialog(
            barrierDismissible: false,
            context: context,
            builder: (context) {
              return const Center(
                child:CircularProgressIndicator(),
              );
            });
    
        await FirebaseAuth.instance.signInWithEmailAndPassword(
          email: emailController.text.trim(),
          password: passwordController.text.trim(),
        );
        Navigator.of(context)
                .pushNamedAndRemoveUntil('/home', (route) => false);
      } on FirebaseAuthException catch (e) {
        if (e.code == 'user-not-found') {
           ScaffoldMessenger.of(context).showSnackBar(
           const SnackBar(content: Text('user-not-found')),
            );
          Navigator.of(context).pop();
        } else if (e.code == 'wrong-password') {
          ScaffoldMessenger.of(context).showSnackBar(
           const SnackBar(content: Text('wrong-password')),
            );
          Navigator.of(context).pop();
          
      }
    
    Login or Signup to reply.
  2. First declare a variable to hold errorText;

    String? errorText;
    

    Then extract the error like this,

    try {
      // try block
    } on FirebaseAuthException catch (e) {
      if (e.code == 'weak-password') {
        errorText = 'The provided password is too weak.';
      } else if (e.code == 'email-already-in-use') {
        errorText = 'The email is already in use.';
      }
      // set the value to your TextEditingController().
      controller.text = errortext ?? "";
    } catch (e) {
      print(e);
    }
    

    You get common error codes from this link,

    https://pub.dev/documentation/firebase_auth/latest/firebase_auth/FirebaseAuth-class.html

    Login or Signup to reply.
  3. Use on FirebaseAuthException catch like this.

    try {
       final newuser = await FirebaseAuth.instance
               .createUserWithEmailAndPassword(
                 email: email ?? 'error',
                 password: password ?? 'error',
            );
    } on FirebaseAuthException catch (e) {
      if (e.code == 'weak-password') {
        print('The password provided is too weak.');
      } else if (e.code == 'email-already-in-use') {
        print('The account already exists for that email.');
      }
    } catch (e) {
      print(e);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search