[英]Flutter - Firebase Auth UID registers as Null in Firestore
[英]Null uid on firestore attempt to save data with flutter and firebase
我试图在用户注册时保存一些数据,但我可能没有调用 FirebaseUser 权限。 有什么建议?
代码:
String errorMessage;
String name;
String password;
String email;
String role;
bool obscure = true;
FirebaseUser instructor;
final FirebaseAuth _auth = FirebaseAuth.instance;
final db = Firestore.instance;
void saveData() async {
await db.collection("instructori")
.document(instructor.uid)
.setData(
{
'Nume': name,
'Rol': role,
'Email': email,
'Parola': password,
'UID': instructor.uid
}
);
}
Future<void> registerUser() async {
instructor = await _auth
.createUserWithEmailAndPassword(
email: email,
password: password,
).then((onValue) {
saveData();
Navigator.of(context).pushNamed(ClientiPage.id);
}).catchError((error) {
errorMessage = error;
});
Navigator.of(context).pushNamed(ClientiPage.id);
}
用户已成功通过身份验证,但我没有看到数据被保存到 Firestore 树中。 我在控制台中完成了所有工作,所以我认为这是代码。
错误:
E/flutter ( 5527): Tried calling: uid
E/flutter ( 5527): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
E/flutter ( 5527): #1 _RegisterPageState.saveData
package:euroswimming_instructori/register_page.dart:29
E/flutter ( 5527): #2 _RegisterPageState.registerUser.<anonymous closure>
package:euroswimming_instructori/register_page.dart:48
E/flutter ( 5527): #3 _rootRunUnary (dart:async/zone.dart:1134:38)
你应该做:
instructor = (await FirebaseAuth.instance.currentUser()).uid;
能够获取用户的uid
。
currentUser
返回Future<FirebaseUser>
类型的值,因此您可以使用await
来检索currentUser
。
https://dart.dev/codelabs/async-await#working-with-futures-async-and-await
注意:
uid
是String
类型,因此如果您希望instructor
是FirebaseUser
类型,请执行以下操作:
instructor = await FirebaseAuth.instance.currentUser();
String userId = instructor.uid;
.then()
返回了一个不同的Future<void>
,所以instructor
被设置为null
。 您可以像这样在.then()
回调中设置instructor
:
void registerUser() {
_auth.createUserWithEmailAndPassword(
email: email,
password: password,
).then((result) {
instructor = result.user;
saveData();
Navigator.of(context).pushNamed(ClientiPage.id);
}).catchError((error) {
errorMessage = error;
});
Navigator.of(context).pushNamed(ClientiPage.id);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.