扑火基地身份验证问题iniside形式

时间:2020-03-05 16:36:33

标签: flutter

您好,我正在尝试构建一个在firebase上集成flutter的身份验证系统。我已经正确集成了所有的firebase类。并且在我的注册页面中,我声明了Firebase的身份验证使用了字符串电子邮件

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

我还建立了一个表格,但是当我按下用于注册用户的确认按钮时,我遇到了这个问题。

  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();

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

我认为问题在于表单上的onSaved里面没有获取电子邮件值。 谢谢

validateAndSubmit()函数:

 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();
  });
}


 }

错误:

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

1 个答案:

答案 0 :(得分:0)

此处您尚未保存表单状态,因此请先将表单状态另存为

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();
  });
}


 }