ios - 接收器 :null error when trying to save form using globalKey

标签 ios firebase flutter google-cloud-firestore

我正在关注这个 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()

此错误消息是我得到的。不知何故,我明白我必须初始化接收者对象(?),但我对如何初始化感到困惑。感谢您的帮助!

最佳答案

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

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

来自博文.. enter image description here

关于ios - 接收器 :null error when trying to save form using globalKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56411935/

相关文章:

ios - icarousel viewForItemAtIndex没有被调用?

flutter - Navigator.pushNamed 在 Flutter 中不起作用

ios - 如何“预填充” GPUImage滤镜以加快加载时间

ios - 在 iOS 中集成 Google 和 Facebook 登录 - AppDelegate openURL

java - 如何从 Firebase 中检索 ListView 中的数据

Javascript - 使用 bool 值显示表数据以追加或创建新表

javascript - 我的谷歌云功能有 promise 问题

flutter - 如何修改整个应用程序的 Scaffold 小部件

android - 在 Flutter 中播放自定义声音

ios - 用户界面 TableView : Change cell height dynamically when clicked the custom button