Fetex
Fetex

Reputation: 108

flutter firebase authentication problem iniside form

I'm trying to build an authentication system with flutter integrating on firebase. I have integrated correctly all the firebase class exc. and in my registration page I have declared the String email which is used by the auth of firebase

      class _RegisterState extends State<Register> {
      String = email;

I have also built a form but when I press the confirmation button for registering the user I'm getting this problem.

  Widget _buildSignIn(BuildContext context) {
return Form(
  key: _formKey2,
  child: Column(
    children: <Widget>[
      Stack(
        alignment: Alignment.topCenter,
        overflow: Overflow.visible,
        children: <Widget>[
          Card(
            elevation: 2.0,
            color: Colors.white,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(8.0),
            ),
            child: Container(
              width: 300.0,
              height: 230.0,
              child: Column(
                children: <Widget>[
                  Padding(
                    padding: EdgeInsets.only(
                        top: 20.0, bottom: 20.0, left: 25.0, right: 25.0),
                    child: TextFormField(
                      onSaved: (value) {email = value;},
                      focusNode: myFocusNodeEmailLogin,
                      controller: loginEmailController,
                      keyboardType: TextInputType.emailAddress,
                      style: TextStyle(
                          fontFamily: "WorkSansSemiBold",
                          fontSize: 16.0,
                          color: Colors.black),
                      decoration: InputDecoration(
                        border: InputBorder.none,
                        icon: Icon(
                          FontAwesomeIcons.envelope,
                          color: Colors.black,
                          size: 22.0,
                        ),
                        hintText: "Email unito",
                        hintStyle: TextStyle(
                            fontFamily: "WorkSansSemiBold", fontSize: 17.0),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
          Container(
            margin: EdgeInsets.only(top: 210.0),
            decoration: new BoxDecoration(
              borderRadius: BorderRadius.all(Radius.circular(5.0)),
              boxShadow: <BoxShadow>[
                BoxShadow(
                  color: Theme.Colors.u,
                  offset: Offset(1.0, 3.0),
                ),
              ],
            ),
            child: MaterialButton(
                highlightColor: Colors.transparent,
                child: Padding(
                  padding: const EdgeInsets.symmetric(
                      vertical: 12.0, horizontal: 42.0),
                  child: Text(_isLoginForm ? 'Login' : 'Create account',
                    style: TextStyle(
                        color: Colors.white,
                        fontSize: 25.0,
                        fontFamily: "WorkSansBold"),
                  ),
                ),
                onPressed: () {
    
                        validateAndSubmit();

                }
          ),
          ),
        ],
      ),
    ],
  ),
);
 }

I'm thinking that the problem in inside the onSaved on the form which is not getting the email value.

validateAndSubmit() function : 

     void validateAndSubmit() async {

    String userId = "";

    try {
      if (_isLoginForm) {
        userId = await widget.auth.signIn(email, _password);
        print('Signed in: $userId');
      } else {
        userId = await widget.auth.signUp(email, _password);
        //widget.auth.sendEmailVerification();
        //_showVerifyEmailSentDialog();
        print('Signed up user: $userId');
      }
      setState(() {
        _isLoading = false;
      });

      if (userId.length > 0 && userId != null && _isLoginForm) {
        widget.loginCallback();
      }
    }catch (e) {
      print('Error: $e');
      setState(() {
        _isLoading = false;
        _errorMessage = e.message;
        _formKey2.currentState.reset();
      });
    }


     }

ERROR :

I/flutter ( 3037): Error: 'package:firebase_auth/src/firebase_auth.dart': Failed assertion: line 174 pos 12: 'email != null': is not true.

Upvotes: 0

Views: 44

Answers (1)

Khadga shrestha
Khadga shrestha

Reputation: 1180

Here you have not save form state so first save form state as

void validateAndSubmit() async {

String userId = "";

_formKey2.currentState.save();

try {
  if (_isLoginForm) {
    userId = await widget.auth.signIn(email, _password);
    print('Signed in: $userId');
  } else {
    userId = await widget.auth.signUp(email, _password);
    //widget.auth.sendEmailVerification();
    //_showVerifyEmailSentDialog();
    print('Signed up user: $userId');
  }
  setState(() {
    _isLoading = false;
  });

  if (userId.length > 0 && userId != null && _isLoginForm) {
    widget.loginCallback();
  }
}catch (e) {
  print('Error: $e');
  setState(() {
    _isLoading = false;
    _errorMessage = e.message;
    _formKey2.currentState.reset();
  });
}


 }

Upvotes: 0

Related Questions