简体   繁体   English

如何在 flutter 中没有未来构建器的情况下从 Firestore 获取单个用户数据

[英]How to get a single user data from firestore without future builder in flutter

I have this in my Auth Controller我在我的 Auth Controller 中有这个

  Future<Member?> readLoggedMemberonly() async {
    final User? user = Auth().currentUser;
    final mid = user?.uid;
    final docMember =
        FirebaseFirestore.instance.collection('users').doc(mid.toString());

    final snapshot = await docMember.get();

    if (snapshot.exists) {
      return Member.fromJson(snapshot.data()!);
    } else {
      return null;
    }
  }

and in my View I have this在我看来我有这个

  final mem = Auth().readLoggedMemberonly;

Now I want to access users data like name现在我想访问名称等用户数据

I tried with我试过了

 child: Text(mem?.name), //name was declared in the model and it exits in the firestore users document

But got this error但是得到了这个错误

在此处输入图像描述

this after removing const from DrawerDrawer中删除const后的 this

在此处输入图像描述

  final mem = Auth().readLoggedMemberonly;

if you want to call a function you have to add brackets如果你想调用 function 你必须添加括号

  final mem = Auth().readLoggedMemberonly();

Then, its future, so probably you want to await for it然后,它的未来,所以可能你想等待它

  final mem = await Auth().readLoggedMemberonly();

then you can use your value那么你可以使用你的价值

  Text(mem?.name ?? '');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Flutter 和 Future Builder 从 Cloud Firestore 正确检索数据? - How to properly retrieve data from Cloud Firestore using Flutter and Future Builder? Listview 构建器与 future 构建器一起使用来自 firestore 的数据 - Listview builder using with future builder with data from firestore 如何从 flutter 中 user.uid 等于文档 ID 的 Cloud firestore 获取数据? - how to get data from cloud firestore where user.uid equal to document id in flutter? 如何使用 flutter 从 firestore 数据库获取嵌套数据? - How to get nested data from firestore data base using flutter? 如何从 flutter 中的 firestore 文档字段获取数据? - How to get data from firestore document fields in flutter? 如何从Flutter中的云Firestore中获取、设置、更新和删除数据? - How to get, set, update and delete data from the cloud firestore in Flutter? 从 Firestore 的 Flutter 中的 StreamSubscription 获取数据 - Get Data from StreamSubscription in Flutter from Firestore Stream builder 从 firestore 到 flutter - Stream builder from firestore to flutter 如何从 Firestore flutter 的参考字段中获取值并在 stream 构建器中显示? - How to get value from reference field in firestore flutter and display in stream builder? 需要从 Firestore 获取正确的数据 Flutter - Need to get correct data from Firestore Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM