Flutter Firebase 用户文档 ID 和 UID 不同

问题描述 投票:0回答:1

我有一个集合“用户”,该用户的文档ID与当前用户的UID不同。因此,当我在以下代码中使用 FirebaseAuth.instance.currentUser!.uid 时,它不起作用。我正在使用匿名登录来创建用户,不确定这是否会产生影响。

FirebaseFirestore.instance.collection('user').doc(FirebaseAuth.instance.currentUser!.uid).collection('subcollection')

文档ID

enter image description here

UID

enter image description here

如果有人能澄清的话,我将非常感激:

  • 如果这是使 docid 和 uid 不同的正常 Firebase 行为。
  • 该场景下如何访问当前用户的数据。
  • 如何使文档id和uid相同。
flutter firebase google-cloud-firestore firebase-authentication
1个回答
0
投票

以下代码创建名称为 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.");
    }
  }
© www.soinside.com 2019 - 2024. All rights reserved.