嵌套 ListView、StreamBuilder 和子集合

2024-04-30

我正在尝试创建一个嵌套的 ListView,但不知道是否可以在嵌套视图中组合 2 个流构建器,因为它不起作用。在带有子集合查询的第二个 StreamBuilder 中,似乎没有返回数据,我不明白为什么。

当我对文档 ID 进行硬编码时,我没有收到任何错误,但查询似乎仍然没有返回任何数据。

有谁知道如何使用streambuilders和Firestore构建嵌套列表视图?

  List<Widget> buildStreamedListView() {
return [ StreamBuilder(
    stream: Firestore.instance.collection('course')
              .document(widget.data.documentID)
              .collection('section')
              .snapshots(),
    builder: (context, snapshot) {
      if (!snapshot.hasData) return const Text("Loading...");
      return Expanded(child: ListView.builder(
        itemCount: snapshot.data.documents.length,
        itemBuilder: (context, index) {
          //return buildListItem(context, snapshot.data.documents[index]);

          return Card(
            child: ExpansionTile(
              title: Text(snapshot.data.documents[index]['name']),
              children: <Widget>[
                StreamBuilder(
                  stream: Firestore.instance.collection('course')
                    .document(widget.data.documentID)
                    .collection('section')
                    .document('4CjAZEQ416NYpu3ra3OE')
                    .collection('page')
                    .snapshots(),
                  builder: (context, snap) {
                    return ListView.builder(
                      shrinkWrap: true,
                      itemCount: snap.data.documents.length,
                      itemBuilder: (context, index) {
                        return Text('Hello you');
                      }
                    );
                  }
                ),
              ],
            ),
          );
        }
      ));
    },
  )];

}


我遇到奇怪错误的原因是,在第二个构建器函数中我没有添加以下代码:

if (!snapshot.hasData) return const Text("Loading...");

一旦我添加它,它就起作用了。似乎数据还没有准备好,因此无法读取,因此出现错误。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

嵌套 ListView、StreamBuilder 和子集合 的相关文章

随机推荐