我试图在用户首次注册应用程序时将用户凭据(姓名和电子邮件)存储到Cloud Firestore。
我能够创建新用户,但有麻烦注册凭据到firestore。有人能告诉我我做错了什么吗??
下面是我注册用户的代码示例。
bool isloggedin = false;
final TextEditingController _email = TextEditingController();
final TextEditingController _pswd = TextEditingController();
final TextEditingController _name = TextEditingController();
handleSubmit() {
if (formkey.currentState!.validate()) {
setState(() {
isloggedin = !isloggedin;
});
Auth()
.registerWithEmailAndPassword(
name: _name.text, email: _email.text, password: _pswd.text)
.then((value) async {
await FirebaseFirestore.instance
.collection("users")
.doc(value?.uid)
.set({
'uid': value?.uid,
'email': _email,
'name': _name,
'password': _pswd
});
});
if (FirebaseAuth.instance.currentUser != null) {
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (BuildContext context) => MyHomePage()));
} else {
setState(() {
isloggedin = !isloggedin;
});
showDialog(
context: context,
builder: (context) {
return AlertDialog(
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text(
'Okay',
style: TextStyle(color: Colors.blue),
))
],
title: const Text('Already Registered'),
content: const Text(
'This Email is already registered with us. Try signing in'),
);
});
}
}
}
handleSubmit函数在用户单击创建帐户按钮时调用。
用户注册了,但应用程序并没有向前移动,但当我按回手机时,我到达了主页,所以我知道我已经注册了,而且我可以在firebase auth控制台上看到我的电子邮件,但在cloud firestore中没有任何新内容。
2条答案
按热度按时间cngwdvgl1#
所以,问题是条件是不是等待你调用完成。下面的代码将解决您的问题
因此,在代码中,我移动了
if (FirebaseAuth.instance.currentUser != null)
,就在您在firestorm和then
中添加数据之后。mu0hgdu02#
实际上我找到了解决方案。我必须在firebase.dart文件中添加firestore代码,而不是之前的地方(signup.dart文件),就在我调用firebaseAuth的createUserWithEmailAndPassword方法的部分之后。