Flutter streambuilder返回错误的标题

lmvvr0a8  于 2022-11-30  发布在  Flutter
关注(0)|答案(1)|浏览(122)

我在一个streambuilder中从firebase接收“title”,但是它从所有文档中获得所有标题。我只想从选定的文档中获得标题。
第一次

StreamBuilder(
                      stream: Firestore.instance
                          .collection(widget.user.uid)
                          .orderBy('date', descending: true)
                          .snapshots(),
                      builder: (BuildContext context,
                          AsyncSnapshot<QuerySnapshot> snapshot) {
                        if (snapshot.connectionState ==
                            ConnectionState.waiting) {
                          return const Center(
                            child: CircularProgressIndicator(),
                          );
                        }

                        if (snapshot.hasData) {
                          return ListView(
                            shrinkWrap: true,
                            children: snapshot.data.documents.map((document) {
                              final current = document.data;
                              final activities = current["activities"] as List;
                              List titles =
                                  activities.map((e) => e["title"]).toList();

                              return Center(
                                child: Container(
                                  width:
                                      MediaQuery.of(context).size.width / 1.2,
                                  height:
                                      MediaQuery.of(context).size.height / 6,
                                  child: Text("Title: $titles"),
                                ),
                              );
                            }).toList(),
                          );
                        }

                        return const Text("no data");
                      },
                    )

我做错了什么?我只想显示标题:Lkjn和标题:列表视图中99okkj。

gorkyyrv

gorkyyrv1#

只需在activitites.map(...)操作中创建小部件,并使您的小部件列表平坦
以下是build方法的更新版本:

Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
      shrinkWrap: true,
      children: snapshot.data.documents.map((document) {
        final current = document.data;
        final activities = current["activities"] as List;

        return activities.map((e) => Center(
          child: Container(
            width:
            MediaQuery.of(context).size.width / 1.2,
            height:
            MediaQuery.of(context).size.height / 6,
            child: Text("Title: ${e["title"]}"),
          ),
        )

        );
      }).expand((element) => element).toList(),
    ));
  }

相关问题