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