Home > OS >  I can't display the firebase auth exceptions in my flutter app
I can't display the firebase auth exceptions in my flutter app

Time:04-26

Everything works fine,i can smoothly create a user but i'm stuck on displaying the signup exceptions on my app.

Please check what is missing my code

    class MyEmail extends StatefulWidget {
     @override
     State<MyEmail> createState() => _MyEmailState(nameController);
    }

   class _MyEmailState extends State<MyEmail> {
   String errorMessage = '';
   ...

    @override
   Widget build(BuildContext context) {
   return GestureDetector(
    ..
          Form(
              key: _regFormKey,   
                Container(
                  margin: EdgeInsets.all(20),
                  child: TextFormField(
                    controller: _emailController,
                    focusNode: _focusEmail,
                    validator: (value) => Validator.validateEmail(
                      email: value,
                    ),
                    decoration: InputDecoration(
                      hintText: "Email",
                  ),
                ),
                ), // end of form
                   Center(
                  child: Text(
                 errorMessage,
                style: TextStyle(color: Colors.red),
               ),
               ),

           ...
      }

         

Here is my button

RaisedButton(
              onPressed: () async {
                if (_regFormKey.currentState!.validate()) {
                  setState(() {
                    _isProcessing = true;
                  });

                  try {
                    User? user = await FireAuth.registerUsingEmailPassword(
                      name: nameController,
                      email: _emailController.text,
                      password: _passwordController.text,
                    );
                    setState(() {
                      _isProcessing = false;
                    });
                    errorMessage = '';

                    if (user != null) {
                      Navigator.of(context).pushAndRemoveUntil(
                        MaterialPageRoute(
                          builder: (context) => ProfilePage(user: user),
                        ),
                        ModalRoute.withName('/'),
                      );
                    }
                  } on FirebaseAuthException catch (error) {
                    errorMessage = error.message!;
                  }
                 
                }
              },
             
              child: const Text(
                'Continue',
                style: TextStyle(color: Colors.white, fontSize: 20),
              ),
            ),          

I'm using the string errorMessage to carry the auth exceptions that are later supposed to be displayed before the continue button. The exceptions are shown on my IDE output but the problem is displaying them on the app

Everything else on creating the user works just fine .

CodePudding user response:

Instead of

on FirebaseAuthException catch (error) { errorMessage = error.message!; }

Use

on FirebaseAuthException catch (error) { setState(() { errorMessage = error.message!; }); }

CodePudding user response:

Use SnackBar to show error messages.

 ScaffoldMessenger.of(context).showSnackBar(SnackBar(
          content: Text('${errorMessage}'),
          backgroundColor: Colors.red,
        ));
  • Related