【发布时间】:2021-01-08 06:58:17
【问题描述】:
我想在 Firestore 上添加数据,但它不起作用。有人可以帮助我吗?
这是最新的更新版本,我不知道如何...
firebase_auth: ^0.18.0+1 cloud_firestore: ^0.14.0+2
这是注册屏幕,所以我想在创建电子邮件和密码后发送数据。 我也想添加带有用户 uid 的文档。
onPressed: () async {
try {
UserCredential userCredential = await FirebaseAuth
.instance
.createUserWithEmailAndPassword(
email: _emailController.text,
password: _passwordController.text,
);
if (userCredential != null) {
firestore
.collection("user")
.doc('user.uid')
.set({
'username': username,
'email': email,
})
.then((value) => print("User Added"))
.catchError((error) =>
print("Failed to add user: $error"));
Navigator.of(context).pushNamed(AppRoutes.authLogin);
}
} catch (e) {
print(e);
_usernameController.text = "";
_passwordController.text = "";
_repasswordController.text = "";
_emailController.text = "";
//TODO: alertdialog with error
}
setState(() {
saveAttempted = true;
});
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
}
},
有人可以帮我做firestore吗..谢谢..
【问题讨论】:
-
使用 if (_formKey.currentState.validate()) { _formKey.currentState.save(); } 来包装函数的其余部分......这样你首先保存表单,以便你可以发布它的数据
-
要按用户 ID 存储用户文档,您需要从 firebase 获取用户信息。在那里,您将获得用户 ID。我在下面分享了综合代码
标签: firebase flutter google-cloud-firestore