firebase 检索存储在用户集合下的配方数据列表?[副本]

mzsu5hc0  于 2023-05-01  发布在  其他
关注(0)|答案(1)|浏览(133)

此问题已存在

How can i retrieve a list of recipe data document(which is a field value) that store under a specific user document?
7天前关闭
我在Firebase中有两个集合,一个是users集合,我存储了所有用户的详细信息,另一个集合是colrecipe,每个用户都有一个子集合。uid作为文档。我正在尝试基于数组列表获取Firestore集合。
如何从用户集合中检索这些配方的详细信息?

body: StreamBuilder(
  stream: Firestore.instance
      .collection('users')
      .document(globals.user.email)
      .collection('colrecipes')
      .snapshots(),
  builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
    if (!snapshot.hasData) {
      return Center(
        child: CircularProgressIndicator(),
      );
    }

    return ListView.builder(
        itemCount: snapshot.data!.documents.length,
        itemBuilder: (context, index) {
          final DocumentSnapshot doc =
              snapshot.data!.documents[index];

               if ((recipemodel.recipeName)!.isEmpty) {
                      return Card(
                          margin: const EdgeInsets.all(4.0),
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(20),
                          ),
                          elevation: 20,
                          shadowColor: Color.fromARGB(255, 0, 0, 0),
                          child: GestureDetector(
                            onTap: () => navigateToInfo(doc),
                            onTapDown: (details) => _getTapPosition(details),
                            onLongPress: () => _showContextMenu(context),
                            child: Container(
                              child: ClipRRect(
                                borderRadius: BorderRadius.circular(20.0),
                                child: Column(
                                  mainAxisSize: MainAxisSize.max,
                                  children: <Widget>[
                                    Stack(
                                      children: <Widget>[
                                        Container(
                                            height: MediaQuery.of(context)
                                                    .size
                                                    .height *
                                                0.17,
                                            decoration: BoxDecoration(
                                              image: DecorationImage(
                                                image: NetworkImage(
                                                  '${doc["image"]}' +
                                                      '?alt=media&token',
                                                ),
                                                fit: BoxFit.cover,
                                              ),
                                            )),
                                      ],
                                    ),
                                    Container(
                                      padding: EdgeInsets.symmetric(
                                          horizontal: 8, vertical: 6),
                                      height:
                                          MediaQuery.of(context).size.height *
                                              0.05,
                                      width: MediaQuery.of(context).size.width,
                                      decoration: BoxDecoration(
                                        color:
                                            Color.fromARGB(146, 205, 204, 204),
                                      ),
                                      child: Align(
                                        alignment: Alignment.centerLeft,
                                        child: Text(
                                          doc["recipeName"],
                                          style: TextStyle(
                                            overflow: TextOverflow.clip,
                                            color:
                                                Color.fromARGB(255, 51, 44, 25),
                                            fontWeight: FontWeight.bold,
                                            fontSize:
                                                displayWidth(context) * 0.050,
                                          ),
                                        ),
                                      ),
                                    ),
                                    Container(
                                      padding: EdgeInsets.all(5),
                                      height:
                                          MediaQuery.of(context).size.height *
                                              0.06,
                                      decoration: BoxDecoration(
                                        color: Color.fromARGB(255, 98, 91, 94),
                                      ),
                                      child: Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.spaceEvenly,
                                        children: [
                                          Row(
                                            children: [
                                              Transform.scale(
                                                scale: 1.4,
                                                child: Icon(
                                                  Icons.room_service,
                                                  color: Colors.white,
                                                ),
                                              ),
                                              SizedBox(
                                                width: 10,
                                              ),
                                              Text(
                                                doc["recipeServe"],
                                                style: TextStyle(
                                                  fontSize:
                                                      displayWidth(context) *
                                                          0.045,
                                                  color: Colors.white,
                                                ),
                                              ),
                                            ],
                                          ),
                                          Row(
                                            children: [
                                              Transform.scale(
                                                scale: 1.4,
                                                child: Icon(
                                                  Icons.access_time_filled,
                                                  color: Color.fromARGB(
                                                      255, 255, 255, 255),
                                                ),
                                              ),
                                              SizedBox(
                                                width: 10,
                                              ),
                                              Text(
                                                doc["recipeTimePrepared"],
                                                style: TextStyle(
                                                  fontSize:
                                                      displayWidth(context) *
                                                          0.045,
                                                  color: Colors.white,
                                                ),
                                              ),
                                            ],
                                          )
                                        ],
                                      ),
                                    )
                                  ],
                                ),
                              ),
                            ),
                          ));
                    }
                  }

9nvpjoqh

9nvpjoqh1#

问题是你的colrecipes id在users中不是一个集合,把它作为一个集合并命名为iit为recipeIds,然后下面的代码就可以工作了。

FirebaseFirestore.instance
        .collection("users")
        .doc(globals.user.email)
        .collection("recipeIds")
        .snapshots();

如果你不想做一个集合,那么就像这样更新你的代码

FirebaseFirestore.instance
            .collection("users")
            .doc(globals.user.email)
            .get();

在您的列表视图中。构建器使用如下

ListView(
                  children:
                      snapshot.data!.docs.map((DocumentSnapshot document) {
                    Map<String, dynamic> data =
                        document.data()! as Map<String, dynamic>;
                    return Text(data[recipeIds]);
                  }).toList(),
                );

相关问题