【问题标题】:How to list favorites/bookmarks in Flutter with Firestore如何使用 Firestore 在 Flutter 中列出收藏夹/书签
【发布时间】:2022-01-01 22:11:56
【问题描述】:

我有一个名为 Stuff 的集合,其中包含一个标题。将其视为 Twitter 帖子。

{
    'stuffID': string
    'title': string
    'details': string
}

我有一个收藏夹集合,可以保存谁喜欢哪个帖子。一个用户可以收藏多个内容。

{
    'userID': string
    'stuffID': string
}

从第二个集合中,我想获取当前用户最喜欢的所有 stuffID。我想用这些从第一次收集中获取其余信息。总之,我想列出所有用户最喜欢的东西。就像一个书签列表。

我认为我必须使用两个 StreamBuilder 来实现这一点。但我无法让它工作。

这是我设法做的:

  @override
  Widget build(BuildContext context) {
    final FirebaseAuth auth = FirebaseAuth.instance;
    final User? user = auth.currentUser;
    final userID = user!.uid;
    var resultStream = FirebaseFirestore.instance
        .collection('favorites')
        .where("userID", whereIn: [userID]).snapshots();

    return StreamBuilder<QuerySnapshot>(
        stream: resultStream,
        builder:
            (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot1) {
          if (snapshot1.hasError) {
            return Text('Something is wrong.');
          }

          if (snapshot1.connectionState == ConnectionState.waiting) {
            return Text("Loading");
          }
          snapshot1.data!.docs.map((DocumentSnapshot document1) {
            Map<String, dynamic> data1 =
                document1.data()! as Map<String, dynamic>;
            print(data1['stuffID']);
            Query _stuffStream = FirebaseFirestore.instance
                .collection('Stuffs')
                .where('stuffID', isEqualTo: data1['stuffID']);
            return StreamBuilder<QuerySnapshot>(
                stream: _stuffStream.snapshots(),
                builder: (BuildContext context,
                    AsyncSnapshot<QuerySnapshot> snapshot2) {
                  if (snapshot2.hasError) {
                    return Text('Something is wrong.');
                  }

                  if (snapshot2.connectionState == ConnectionState.waiting) {
                    return Text("Loading");
                  }

                  return ListView(
                    //showing the data
                    children:
                        snapshot2.data!.docs.map((DocumentSnapshot document) {
                      Map<String, dynamic> data =
                          document.data()! as Map<String, dynamic>;
                      String stuffID = data['stuffID'];
                      return ListTile(
                        title: Text(data['title']),
                        subtitle: Text(data['details']),
                      );
                    }).toList(),
                  );
                });
          });
          return const Center(child: CircularProgressIndicator());
        });
  }

当我使用此代码时,应用程序卡在加载屏幕:

我尝试了两天,但我所有的尝试都失败了。你能帮忙吗?

【问题讨论】:

    标签: flutter dart google-cloud-firestore stream-builder


    【解决方案1】:

    我日复一日地进行了越来越多的研究并找到了答案。我创建了基于here. 的代码,请注意,我在 Firebase 中更改了我的集合的设计。但它与其余代码完全无关。我只是想用更有效的方式来存储数据。

    总之,我正在从收藏夹集合中获取 stuffID。并使用那个 stuffID 来获取东西。

    @override
      Widget build(BuildContext context) {
        final FirebaseAuth auth = FirebaseAuth.instance;
        final User? user = auth.currentUser;
        final userID = user!.uid;
    
        return StreamBuilder<QuerySnapshot>(
          stream: FirebaseFirestore.instance
              .collection('favorites')
              .doc(userID)
              .collection(userID)
              .snapshots(),
          builder: (context, snapshot) {
            if (!snapshot.hasData) return const Text("Loadsing...");
            return Column(children: [
              ListView.builder(
                  scrollDirection: Axis.vertical,
                  shrinkWrap: true,
                  itemCount: snapshot.data!.docs.length,
                  itemBuilder: (context, index) {
                    //return buildListItem(context, snapshot.data.documents[index]);
                    return ListView(
                      scrollDirection: Axis.vertical,
                      shrinkWrap: true,
                      children: <Widget>[
                        StreamBuilder<QuerySnapshot>(
                            stream: FirebaseFirestore.instance
                                .collection('Stuffs')
                                .where('stuffID',
                                    isEqualTo: snapshot.data!.docs[index]
                                        ['stuffID']) //seçilen döküman
                                .snapshots(),
                            builder: (context, snap) {
                              if (!snap.hasData) return const Text("Loading...");
                              return ListTile(
                                leading: CircleAvatar(
                                  backgroundImage: NetworkImage(
                                      snap.data!.docs[index]['stuffImage']),
                                ),
                                title: Text(snap.data!.docs[index]['title']),
                                subtitle: Column(
                                  children: <Widget>[
                                    Text(snap.data!.docs[index]['details']),
                                  ],
                                ),
                              );
                            }),
                      ],
                    );
                  }),
            ]);
          },
        );
      }
    

    【讨论】:

      猜你喜欢
      • 2020-10-04
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-05
      • 1970-01-01
      相关资源
      最近更新 更多