【问题标题】:How to use .document and current user correctly?如何正确使用 .document 和当前用户?
【发布时间】:2020-12-07 13:22:17
【问题描述】:

我有一些代码:

StreamBuilder<DocumentSnapshot>(
                                      stream: Firestore.instance
                                          .collection('business')
                                          .document(
                                              'XxLqHwyhgAWlnSGQ3rXOBAdY5Rs1')
                                          .snapshots(),
                                      builder: (context, snapshot) {
                                        Map<String, dynamic>
                                            documentFields =
                                            snapshot.data.data;
                                        return Text(
                                          documentFields["name"],
                                          style: TextStyle(
                                            fontSize: 21,
                                            fontWeight: FontWeight.w500,
                                          ),
                                        );
                                      }),

这里我必须使用 currentUser():

.document('XxLqHwyhgAWlnSGQ3rXOBAdY5Rs1')

但我不知道怎么做,因为它是文本小部件。请大家帮帮我

【问题讨论】:

  • 这是XxLqHwyhgAWlnSGQ3rXOBAdY5Rs1的用户ID吗?
  • 是的,它是用户 ID
  • 不清楚您要做什么? currentUser() 是一个返回未来的方法。如果你想有一个流,你必须使用生成器

标签: firebase flutter dart google-cloud-firestore firebase-authentication


【解决方案1】:

您必须使用 Firebase Auth 获取 currentUser(),因为您使用的是 StreamBuilder 小部件,然后首先创建一个返回 Stream 的方法:

  Stream<DocumentSnapshot> getData() async* {
    FirebaseUser user = await FirebaseAuth.instance.currentUser();
    yield* Firestore.instance
        .collection('business')
        .document(user.uid)
        .snapshots();
  }

然后将方法赋值给属性stream

StreamBuilder<DocumentSnapshot>(
   stream: getData(),
  builder: (context, snapshot) {
   //..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-18
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2019-04-05
    • 2021-03-08
    • 1970-01-01
    相关资源
    最近更新 更多