尝试使用globalKey保存表单时,Receiver:null错误 [英] Receiver:null error when trying to save form using globalKey

查看:43
本文介绍了尝试使用globalKey保存表单时,Receiver:null错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注

I'm following this https://medium.com/@c_innovative/simple-firebase-login-flow-in-flutter-6f44c2b5c58a tutorial and i am having some trouble with getting the login in to work. I have already connected my app to firebase and enabled email and password authentication on firebase console!

I have tried entering the key argument into the TextFormfield but so far im unable to do so.

class LoginPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _LoginPageState();
  }
}

class _LoginPageState extends State<LoginPage> {
  final _formKey = GlobalKey<FormState>();
  String _email;
  String _password;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Login Page Flutter Firebase"),
        ),
        body: Container(
          padding: EdgeInsets.all(20.0),
          child: Column(
            children: <Widget>[
              Spacer(flex: 5),
              Text(
                "Login  Information",
                style: new TextStyle(fontSize: 30.0),
              ),
              Spacer(
                flex: 5,
              ),
              TextFormField(
                key: _formKey,
                onSaved: (value) => _email = value,
                keyboardType: TextInputType.emailAddress,
                decoration: InputDecoration(labelText: "Email Address"),
              ),
              TextFormField(
                onSaved: (value) => _password = value,
                obscureText: true,
                decoration: InputDecoration(labelText: "Password"),
              ),
              Spacer(flex: 3),
              RaisedButton(
                  child: Text("Login"),
                  onPressed: () async {
                    //Getting the error here
                    final form = _formKey.currentState;
                    form.save();

                    if (form.validate()) {
                      print('$_email $_password');
                      var result = await 
                      Provider.of<AuthService(context).loginUser(email: _email, password:_password);
                      if (result == null) {
                        print("result is null");
                      }
                    }
                  }),
              Spacer(
                flex: 20,
              )
            ],
          ),
        ));
  }
}

[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: NoSuchMethodError: The method 'save' was called on null. Receiver: null Tried calling: save()

this error message is what i get. somehow i understand that i have to initialise the receiver object(?) but i am confused on how to do so. Thanks for the help!

解决方案

you put the _formKey on the TextFormField associated with the email field, it doesn't belong there

 body: Container(
            padding: EdgeInsets.all(20.0),
            child: Form(
                key: _formKey,
                child: Column(children: <Widget>[
                  ....
                 ]
            ) // Form
         ) // Container

From the blog post..

这篇关于尝试使用globalKey保存表单时,Receiver:null错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆