我有一个集合“用户”,该用户的文档ID与当前用户的UID不同。因此,当我在以下代码中使用 FirebaseAuth.instance.currentUser!.uid 时,它不起作用。我正在使用匿名登录来创建用户,不确定这是否会产生影响。
FirebaseFirestore.instance.collection('user').doc(FirebaseAuth.instance.currentUser!.uid).collection('subcollection')
文档ID
UID
如果有人能澄清的话,我将非常感激:
以下代码创建名称为 UID 的用户文档。
try {
// Logs in the user anonymously
final userCredential = await FirebaseAuth.instance.signInAnonymously();
// Gets the UID
String uid = userCredential.user!.uid;
// Creates a field inside the user document to store the UID
final userData = {"UID": uid, };
// Creates the user document with the UID as it's name
await FirebaseFirestore.instance.collection('user').doc(uid).set(userData);
print("Signed in with temporary account.");
} on FirebaseAuthException catch (e) {
switch (e.code) {
case "operation-not-allowed":
print("Anonymous auth hasn't been enabled for this project.");
break;
default:
print("Unknown error.");
}
}