Flutter/Firestore-如何在等于当前用户的子集合用户ID中获取集合 [英] Flutter / Firestore - How can i fetch collection in subcollection userid equal to currentuser

查看:39
本文介绍了Flutter/Firestore-如何在等于当前用户的子集合用户ID中获取集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Firestore结构在这里,

My Firestore structure here,

我想获取课程数据.但是只有成员集合中具有相同用户ID的用户.但是我不能.是最差的用法吗?

i want to fetch lesson data.But only users with equal user ids in the member collection.But i can't. is that worst usage?

  body: StreamBuilder(
    stream: firestore
        .collection('lessons')
        .where('members', isEqualTo: FirebaseAuth.instance.currentUser.uid)
        .snapshots(),
    builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
      if (!snapshot.hasData) {
        return Center(
          child: CircularProgressIndicator(),
        );
      } else {
        return Container(
          child: ListView(
            scrollDirection: Axis.vertical,
            children: snapshot.data.docs.map((document) {
              return Card(
                margin: EdgeInsets.all(5),
                child: Column(
                  children: [
                    ListTile(
                      title: Text(document['lessonTitle']),
                      leading: Icon(Icons.label),
                      trailing: Text(document['lessonSubtitle']),
                    )
                  ],
                ),
              );
            }).toList(),
          ),
        );
      }
    },
  ),

推荐答案

无法根据一个(子)集合中的值从一个集合中选择文档.Firestore中的查询只能考虑他们返回的文档中的信息.

There is no way to select documents from one collection based on values in another (sub)collection. Queries in Firestore can only consider information in the documents that they're returning.

如果要根据成员的身份选择课程,则需要将该信息包括在每个课程文档本身中.这样做的常见方法是向每个课程文档添加一个 members 数组,使用 arrayUnion arrayRemove 操作来查询数组.

If you want to select lessons based on their members, you'll need to include that information in each lesson document itself. The common way to do this is to add a members array to each lesson document, use arrayUnion and arrayRemove operations to manipulate that array, and then use array-contains to query the array.

这篇关于Flutter/Firestore-如何在等于当前用户的子集合用户ID中获取集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆