首页 > 解决方案 > 我如何使用路由检索文档 ID,并在详细页面中检索文档 ID 及其子集合的内部

问题描述

这里是主集合
这里是子集合

每个文档都有子集合。我真的不知道如何从 gridview 中检索到带有 firestore 查询的详细信息页面

我真的是这个行业的新手,我真的很抱歉。

有没有办法解决这个问题?在 youtube、google 上找不到。请帮我

              StreamBuilder(
                stream: FirebaseFirestore.instance
                    .collection('locationdata')
                    .snapshots(),
                builder: (context, snapshot) {
                  if (!snapshot.hasData)
                    return Center(child: CircularProgressIndicator());
                  return GridView.builder(
                      primary: false,
                      shrinkWrap: true,
                      physics: NeverScrollableScrollPhysics(),
                      scrollDirection: Axis.vertical,
                      itemCount: snapshot.data.docs.length,
                      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                          crossAxisCount: 2),
                      itemBuilder: (context, index) => _locationListItem(
                          context, snapshot.data.docs[index]));
},
              ),
              SizedBox(height: 30),
            ],
          ),
        ));
  }
}

Widget _locationListItem(BuildContext context, DocumentSnapshot document) {
  return Padding(
    padding: const EdgeInsets.all(8.0),
    child: GestureDetector(
      onTap: () => Navigator.push(context,
          MaterialPageRoute(builder: (context) => DestinationScreen())),
      child: Card(
        clipBehavior: Clip.antiAlias,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(24)),
        child: ClipRRect(
          borderRadius: BorderRadius.circular(20),
          child: Stack(alignment: Alignment.center, children: [
            Image.network(
              document['imgURL'],
              height: 240,
              fit: BoxFit.cover,
            ),
            Positioned(
                bottom: 0,
                child: Container(
                  //padding: EdgeInsets.only(top: 10, bottom: 10, right: 70, left: 70),
                  width: MediaQuery.of(context).size.width,
                  height: MediaQuery.of(context).size.height * 0.05,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(20),
                    color: Colors.grey[600],
                  ),
                  child: Center(
                    child: Text(
                      document['name'],
                      style: TextStyle(
                          fontSize: 15,
                          fontWeight: FontWeight.bold,
                          color: Colors.white),
                    ),
                  ),
                ))
          ]),
        ),
      ),
    ),
  );
}

这是详细页面


    return Scaffold(
      body: StreamBuilder<QuerySnapshot>(
        stream: FirebaseFirestore.instance.collection('locationdata').doc('locationdataDocId').collection('restaurant').snapshots(),
        builder: (BuildContext context,AsyncSnapshot snapshot) {
          if (!snapshot.hasData){
            return Center(child: CircularProgressIndicator());
          }




          return Column(
            children: <Widget> [
              Stack(
                children: <Widget>[
                  Container(
                    height: MediaQuery.of(context).size.width,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(30.0), 
                      boxShadow: [
                        BoxShadow(
                          color: Colors.black26, 
                          offset: Offset(0.0, 2.0), 
                          blurRadius: 6.0),
                          ],
                          ),
                          ),
                          
                          Padding(
                            padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 40.0),
                            child: Row(
                              children: <Widget>[
                                IconButton(
                                  icon: Icon(
                                    Icons.arrow_back,
                                  ),
                                  iconSize: 30.0,
                                  color: Colors.black,
                                  onPressed: () => Navigator.pop(context),
                                )
                              ]
                            ),
                          ),
                ]
              ),
            ]
          );
        }
      ),

      );
  }
}

标签: firebaseflutterdartgoogle-cloud-firestore

解决方案


推荐阅读