【问题标题】:How use the Current Id of an user in .doc()如何在 .doc() 中使用用户的当前 ID
【发布时间】:2021-10-25 23:24:31
【问题描述】:

我正在设置一个用户配置文件,我的 Firestore 中有一个包含第一个“用户”的集合,在这个集合中我有这个用户的 ID,最后我可以显示名称、uid 等。 问题是我想在前端打印该名称,但我无法访问此集合,因为我不知道如何在我的 .doc(uid) 中使用当前 uid,(uid 未定义),我使用了获取uid的未来方法,但我不知道如何连接这些。 希望你能帮助我!

这是我的前端代码

FutureBuilder(
              future: FirebaseFirestore.instance
            .collection('users')
            .doc(uid)
            .get(), //tryna to use that collection but uid is not defined correctly 
              builder: (context, snapshot) {
                if (snapshot.connectionState == ConnectionState.done) {
                  var name = snapshot.data as DocumentSnapshot;
                  return Text(name['displayName'],
                  );
                } else {
                  return Text("Loading...");
                }
              },
            )

和我的提供者当前的用户 ID 代码

 Future<String> inputData()  async {
    final User? user =  _auth.currentUser;
    final uid = user!.uid;
    // here you write the codes to input the data into firestore
    return uid;
  }

我在这里更新了前端代码:

FutureBuilder(
              future: FirebaseFirestore.instance
            .collection('users')
            .doc(FirebaseAuth.instance.currentUser!.uid)
            .get(), //tryna to use that collection but uid is not defined correctly and I need a void but
              // I have one in my auhtprovider
              //solution ? create id from here : use my provider
              builder: (context, snapshot) {
                if (snapshot.hasData)
                  return Text("Loading...");
                if (snapshot.data == null) {
                  print('Document does not exist on the database');
                }else{
                  return Text("Researching data...");
                }
                if (snapshot.connectionState == ConnectionState.done) {
                  var name = snapshot.data as DocumentSnapshot;
                  return Text(name['displayName'],
                  );
                } else {
                  return Text("Loading..");
                }
              },
            )

和 UID ?

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';

Future userSetup(String displayName) async {
  CollectionReference users = FirebaseFirestore.instance.collection('Users');
  FirebaseAuth auth = FirebaseAuth.instance;
  String uid = auth.currentUser!.uid.toString();
  await users.doc(uid).set({'displayName': displayName, 'uid': uid });
  final result = await users.doc(uid).get();
  final data = result.data() as Map<String, dynamic>;
  return data['displayName'];
}

加上我的 Firestore 文档的照片:

【问题讨论】:

  • .doc(FirebaseAuth.instance.currentUser.uid)?
  • 我必须添加“!”在.doc(FirebaseAuth.instance.currentUser!.uid) 中,但它返回错误:类型'Null' 不是类型转换中'DocumentSnapshot' 类型的子类型
  • 所以听起来当前用户没有文档,您的代码无法处理。您需要在访问snapshot.data 之前检查if (snapshot.hasData),然后在访问snapshot.data.data 之前检查该文档是否存在if (snapshot.data.exists)

标签: flutter google-cloud-firestore provider uid


【解决方案1】:

要在读取操作中使用当前用户的 UID,请执行以下操作:

.doc(FirebaseAuth.instance.currentUser.uid)

在那之后你得到的错误,听起来好像没有当前用户的文档,你的代码无法处理。

您需要在访问snapshot.data 之前检查if (snapshot.hasData),以确保AsyncSnapshot 已完成与数据库的通信。

然后在访问snapshot.data!.data()之前检查文档是否存在if (snapshot.data!.exists)

请注意,这与 reading data once 文档中的代码几乎完全相同,因此我强烈建议您检查一下(如果需要,请再次检查)。

【讨论】:

  • 谢谢我没有更多错误,除了它返回给我'数据库中不存在文档',我提出了更多详细信息的条件,你可以检查我的代码,我已经更新
  • 请不要从问题中删除您现有的代码,因为这会使我的回答对未来的访问者毫无意义。如果您想显示更多代码,请将其显示为现有代码的补充,而不是替代。
  • 1) 这看起来像一个倒置条件:if (snapshot.hasData) return Text("Loading..."); 2) 这个if (snapshot.data == null) { 看起来应该是if (snapshot.data!.exists)。 3) 您能否显示您使用的 UID,以及您认为应该从 Firebase 控制台加载的文档的屏幕截图。
  • 我把'== null'因为:没有为'Object'类型定义getter'exists'。和我的uid?这意味着我的 Firestore 收藏的代码?如果是这种情况,我在帖子中添加了一个屏幕,其中包含应该加载的文档。
猜你喜欢
  • 2020-10-05
  • 2022-08-02
  • 2015-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-24
  • 2018-10-03
  • 1970-01-01
相关资源
最近更新 更多