错误:NoSuchMethodError:在null上调用了方法“ signUp”

时间:2020-03-06 23:03:08

标签: flutter

你好,我正在开发一个注册页面(我已经关注了本文:https://medium.com/flutterpub/flutter-how-to-do-user-login-with-firebase-a6af760b14d5

我在另一个dart文件中创建了一个BaseAuth类,其中在rootpage中也是使用firebase进行注册的用户的代码,在我有一些代码可以检查用户是否已登录的代码中,我都非常喜欢该指南。

代码如下:(我删除了一些代码,例如Container TextStyle ....,仅保留了相关代码)

        class Registrati extends StatefulWidget {
          Registrati({Key key, this.auth, this.registerCallback}) : super(key: key);
          final BaseAuth auth;
          final VoidCallback registerCallback;
          @override
          _RegistratiState createState() => new _RegistratiState();
        }

        class _RegistratiState extends State<Registrati>
            with SingleTickerProviderStateMixin {
          final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
          final FocusNode myFocusNodeEmailLogin = FocusNode();
          final FocusNode myFocusNodePasswordLogin = FocusNode();
          final FocusNode myFocusNodeEmail = FocusNode();
          final FocusNode myFocusNodePassword = FocusNode();
          TextEditingController loginEmailController = new TextEditingController();
          TextEditingController loginPasswordController = new TextEditingController();
          String email;
          String _password;
          String _errorMessage;
          String email3 = "@studenti.polito.it";
          String email2 = "@edu.unito.it";
          TextEditingController signupEmailController = new TextEditingController();
          TextEditingController signupPasswordController = new TextEditingController();
          final _formKey = GlobalKey<FormState>();
          final _formKey2 = GlobalKey<FormState>();
          bool isLoading = false;
          bool isLoggedIn = false;
          bool _isLoginForm;
          bool _isLoading;
          bool _obscureTextLogin = true;

          void _toggleLogin() {
            setState(() {
              _obscureTextLogin = !_obscureTextLogin;
            });
          }

          @override
          void dispose() {
            myFocusNodeEmail.dispose();
            myFocusNodePassword.dispose();
            super.dispose();
          }

          @override
          void initState() {
            super.initState();

            _errorMessage = "";
            _isLoginForm = true;
            _isLoading = false;


          }

          void resetForm() {
            _formKey2.currentState.reset();
            _errorMessage = "";
          }

          void toggleFormMode() {
            resetForm();
            setState(() {
              _isLoginForm = !_isLoginForm;
            });
          }

          void validateAndSubmit() async {

            String userId = "";

            try {
              if (_isLoginForm) {
                String userId = await widget.auth.signUp(email, _password);

                print('Signed up user: $userId');

              }
              setState(() {
                _isLoading = false;
              });

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


          }


          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,


                                ),
                              ),
                              Padding(

                                child: TextFormField(
                                  onSaved: (value) {

                                    _password = value;

                                  },

                                  focusNode: myFocusNodePasswordLogin,
                                  controller: loginPasswordController,


                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                      Container(
                        //some code
                        ),
                        child: MaterialButton(

                        child: Text(_isLoginForm ? 'Create account' : 'Login',

                              ),
                            ),
                            onPressed: () {

                              setState(() {
                                _isLoading = true;
                                _errorMessage = "";

                                if (_formKey2.currentState.validate())


                                  _formKey2.currentState.save();

                                   validateAndSubmit();


                              });


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

        }

但是当我按下表单的创建帐户按钮时,出现此错误: 错误:NoSuchMethodError:在null上调用了方法'signUp'。 我想了解我可能在哪里用代码弄错了

谢谢

1 个答案:

答案 0 :(得分:0)

您的widget.auth必须为空。因此,当您向此小部件传递用于auth的值时,该值必须为null才能得到此错误。

所以问题是这样的:

Registrati(auth: someAuth); //someAuth was null