问题描述
我需要从Firestore文档中获取数据,这些文档包含子集合.
I need to fetch data from a firestore documents where these document contain sub-collection.
用户在点击新页面时应获得listTile的详细信息.事情是我在点击时传递文档ID时遇到问题.
user should get details of the listTile on tapping in new page. the thing is I have problem passing the doc id on tapping.
body: StreamBuilder(
stream: Firestore.instance.collection('myMainCollection').snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) return CircularProgressIndicator();
return FirestoreListView(documents: snapshot.data.documents);
},
child: ListView.builder(
itemCount: documents.length,
itemExtent: 90.0,
itemBuilder: (BuildContext context, int index) {
final myListTile = ListTile(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => detailCard(documents[index] ),
),
);
},
在detailPage上,我有以下代码:
on the detailPage i have this code:
body: StreamBuilder(
stream: Firestore.instance.collection('myMainCollection').document("TappedDocID").collection('sub-collection').snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) return CircularProgressIndicator();
return FirestoreListView(documents: snapshot.data.documents);
},
),
您知道如何传递用户点击的doc ID吗?
any idea how to pass the doc id the user taps on ?
推荐答案
您可以做的是在详细信息页面中传递ID,然后在详细信息页面中使用传递的ID来获取数据.
What you can do is pass the id in the details page and then in details page fetch the data using the id you passed.
我正在以相同的方式传递和检索,它对我有用.
I'm passing and retrieving the same way and it's working for me..
示例:
使用导航器将文档ID从列表页面传递到详细信息页面:
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailsPage(
docId: 'theIdYouWantToPass',
),
),
);
详细信息页面:
class DetailsPage extends StatefulWidget {
final String docId;
DetailsPage(
{Key key, @required this.docId})
: super(key: key);
@override
DetailsPageState createState() {
return new DetailsPageState();
}
}
// accessing the doc id in details page like this
class DetailsPageState
extends State<DetailsPage> {
// example if you want to store it in another var
// or you can directly use **this.widget.docId**
var docId = this.widget.docId;
}
这篇关于将当前文档ID传递给detailsPage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!