繁体   English   中英

如何根据在另一个集合中执行的 Streambuilder 查询的结果将所有集合文档数据放入列表视图

[英]How to get all collection document data into a listview based on results from Streambuilder Query performed in another collection

我正在尝试使用 for 循环迭代一个新的 Streambuilder,但它只返回一个结果,而它应该更多。 请查看我的问题和下面的图片以更好地解释它。

我正在使用 Streambuilder 获取一些用户数据。 'user_schools'

用户学校

StreamBuilder<QuerySnapshot>(
    stream: FirebaseFirestore.instance
        .collection('users')
        .doc(uid)
        .collection('user_schools')
        .snapshots(),

然后,根据结果,我想获取所有“school_news”数据,其中 user_schools 等于“reg_schools”

在此处输入图像描述

body: StreamBuilder<QuerySnapshot>(
    stream: FirebaseFirestore.instance
        .collection('users')
        .doc(uid)
        .collection('user_schools')
        .snapshots(),
    builder: (context, snapshot) {
      if (snapshot.hasData) {
        for (int i = 0; i < snapshot.data!.docs.length; i++) {
          return Container(
            height: 500,
            child: StreamBuilder<QuerySnapshot>(
              stream: FirebaseFirestore.instance
                  .collection('reg_schools')
                  .doc(snapshot.data!.docs[i].id)
                  .collection('school_news')
                  .orderBy('created', descending: true)
                  .snapshots(),
              builder: (context, snapshot) {
                if (!snapshot.hasData) {
                  return Center(
                    child: LinearProgressIndicator(),
                  );
                } else
                  return ListView(
                    children: snapshot.data!.docs.map((doc) {
                      return Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Container(
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.all(
                              Radius.circular(7),
                            ),
                            color: Colors.purple,
                            gradient: new LinearGradient(
                              colors: [Colors.blue, Colors.lightBlueAccent],
                            ),
                          ),
                          height: 210,
                          width: 400,
                          child: GestureDetector(
                            onTap: () {},
                            child: Column(
                              children: [
                                ListTile(
                                  subtitle: Text(
                                      snapshot.data!.docs[0]['content']),
                                  title:
                                      Text(snapshot.data!.docs[0]['title']),
                                ),
                                SizedBox(
                                  height: 10,
                                ),
                              ],
                            ),
                          ),
                        ),
                      );
                    }).toList(),
                  );
              },
            ),
          );
        }
      }

      return Center(
        child: Text('Loading '),
      );
    },
  ),

然后我总是只得到一个你而不是基于结果长度的预期结果数。 任何帮助或指导正确的方向将不胜感激。 我对 Flutter 的 For 循环知识不是那么好。 谢谢

你能试试这个吗

StreamBuilder<QuerySnapshot>(
        stream: FirebaseFirestore.instance
            .collection('users')
            .doc(uid)
            .collection('user_schools')
            .snapshots(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
    
            return SingleChildScrollView(
              child: Column(children: snapshot.data!.docs.map((element)=>Container(
                  height: 500,
                  child: StreamBuilder<QuerySnapshot>(
                    stream: FirebaseFirestore.instance
                        .collection('reg_schools')
                        .doc(snapshot.data!.docs[i].id)
                        .collection('school_news')
                        .orderBy('created', descending: true)
                        .snapshots(),
                    builder: (context, snapshot) {
                      if (!snapshot.hasData) {
                        return Center(
                          child: LinearProgressIndicator(),
                        );
                      } else
                        return ListView(
                          children: snapshot.data!.docs.map((doc) {
                            return Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: Container(
                                alignment: Alignment.center,
                                decoration: BoxDecoration(
                                  borderRadius: BorderRadius.all(
                                    Radius.circular(7),
                                  ),
                                  color: Colors.purple,
                                  gradient: new LinearGradient(
                                    colors: [Colors.blue, Colors.lightBlueAccent],
                                  ),
                                ),
                                height: 210,
                                width: 400,
                                child: GestureDetector(
                                  onTap: () {},
                                  child: Column(
                                    children: [
                                      ListTile(
                                        subtitle: Text(
                                            snapshot.data!.docs[0]['content']),
                                        title:
                                            Text(snapshot.data!.docs[0]['title']),
                                      ),
                                      SizedBox(
                                        height: 10,
                                      ),
                                    ],
                                  ),
                                ),
                              ),
                            );
                          }).toList(),
                        );
                    },
                  ),
                )).toList(),),
            );
    
          
          }
    
          return Center(
            child: Text('Loading '),
          );
        },
      )

更新

我尝试了您的回答,但未定义“我”,因此我将其更改为以下内容。

body: StreamBuilder<QuerySnapshot>(
    stream: FirebaseFirestore.instance
        .collection('users')
        .doc(uid)
        .collection('user_schools')
        .snapshots(),
    builder: (context, snapshot) {
      if (snapshot.hasData) {
        for (int i = 0; i < snapshot.data!.docs.length; i++) {
          return SingleChildScrollView(
            child: Column(
              children: snapshot.data!.docs
                  .map((element) => Container(
                        child: StreamBuilder<QuerySnapshot>(
                            stream: FirebaseFirestore.instance
                                .collection('reg_schools')
                                .doc(snapshot.data!.docs[i].id)
                                .collection('school_news')
                                .orderBy('created', descending: true)
                                .snapshots(),
                            builder: (context, snapshot) {
                              if (!snapshot.hasData) {
                                return Center(
                                  child: LinearProgressIndicator(),
                                );
                              } else
                                return ListTile(
                                  title: Text(snapshot.data!.docs.length
                                      .toString()),
                                );
                            }),
                      ))
                  .toList(),
            ),
          );
        }
      }

但是,我仍然只获得一个“school_news”数据,而不是两所学校。 但是当我手动更改 i in.doc(snapshot.data..docs[i].id) 时,我得到了尊重的值。

  StreamBuilder<QuerySnapshot>(
                            stream: FirebaseFirestore.instance
                                .collection('reg_schools')
                                .doc(snapshot.data!.docs[0].id)
                                .collection('school_news')
                                .orderBy('created', descending: true)
                                .snapshots(),

StreamBuilder<QuerySnapshot>(
                            stream: FirebaseFirestore.instance
                                .collection('reg_schools')
                                .doc(snapshot.data!.docs[1].id)
                                .collection('school_news')
                                .orderBy('created', descending: true)
                                .snapshots(),

所以我不确定为什么 for 循环没有通过 StreamBuilder 迭代

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM