【问题标题】:Receiver:null error when trying to save form using globalKeyReceiver:尝试使用 globalKey 保存表单时出现空错误
【发布时间】:2019-10-18 02:21:16
【问题描述】:

我正在关注这个https://medium.com/@c_innovative/simple-firebase-login-flow-in-flutter-6f44c2b5c58a 教程,但我在登录工作时遇到了一些麻烦。我已经将我的应用程序连接到 firebase 并在 firebase 控制台上启用了电子邮件和密码身份验证!

我已经尝试将关键参数输入到 TextFormfield 但到目前为止我无法这样做。

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)] 未处理的异常:NoSuchMethodError:在 null 上调用了方法“保存”。 接收方:空 尝试调用:save()

这个错误信息就是我得到的。不知何故,我明白我必须初始化接收器对象(?),但我对如何做到这一点感到困惑。感谢您的帮助!

【问题讨论】:

    标签: ios firebase flutter google-cloud-firestore


    【解决方案1】:

    您将_formKey 放在与电子邮件字段关联的TextFormField 上,它不属于那里

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

    来自博客文章..

    【讨论】:

    • 嗨亚伦,我添加它以尝试排除故障。我应该把我的_formkey放在哪里?做最终形式 = _formKey.currentState; form.save();行仍然抛出我在 null 上调用 save 的错误!
    • 查看编辑,看起来你错过了博客文章中的一步,虽然你可以使用TextEditingControllers 我认为它使示例复杂化
    • 我明白了!我的大错!感谢您花时间查看我的代码!非常感谢!感谢您指导像我这样的颤振菜鸟>
    【解决方案2】:

    我只使用TextEditingControllers 在您的字段上而不是使用 onSaved,并在您调用您的 signIn 函数时从这些字段中获取文本的值。

    【讨论】:

    • 很高兴它为您服务。如果此答案足够,请不要忘记将问题标记为已解决。
    猜你喜欢
    • 1970-01-01
    • 2017-08-07
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    • 2014-09-05
    • 1970-01-01
    • 2021-02-15
    相关资源
    最近更新 更多