接收器:尝试使用globalKey

时间:2019-06-02 03:12:27

标签: ios firebase flutter google-cloud-firestore

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

我尝试将key参数输入到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上被调用。 接收者:null 尝试调用:save()

此错误消息是我得到的。我以某种方式知道我必须初始化接收器对象(?),但是我对如何初始化感到困惑。感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

您将$latestMessage->id // Prints 2 $latestMessage->sender_id // prints 2 $latestMessage->body // prints 'ok' 放在与电子邮件字段关联的_formKey上,它不属于该字段

TextFormField

来自博客文章。 enter image description here

答案 1 :(得分:0)

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

相关问题