[英]how can i retrieve document id of each document from Firestore in flutter
在这里,我有从 Firebase 检索所有文档的代码,我只能检索文档中存在的数据,但我也想用它检索文档 ID,我该怎么做?
StreamBuilder<QuerySnapshot>(
stream: getData(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError)
return new Text('Error: ${snapshot.error}');
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return new Text('Loading...');
default:
return new ListView(
children: snapshot.data.documents
.map((DocumentSnapshot document) {
return new CustomCard(
name: document['name'],
phone: document['phno'],
service: document['Category'],
address: document['address'],
pin: document['pin'],
payment: document['Payment'],
);
}).toList(),
);
}
},
),
这是我的吸气剂 function
Stream<QuerySnapshot> getData()async*{
FirebaseUser user = await FirebaseAuth.instance.currentUser();
yield* Firestore.instance
.collection('User')
.where("userId", isEqualTo: user.uid)
.orderBy('upload time', descending: true)
.snapshots();
}
要获取 id,请执行以下操作:
.map((DocumentSnapshot document) {
return new CustomCard(
docId : document.documentID,
name: document['name'],
phone: document['phno'],
service: document['Category'],
address: document['address'],
pin: document['pin'],
payment: document['Payment'],
);
}).toList(),
documentID
将为您提供每个文档 ID。
你也可以做类似的事情
snapshot.data!.docs[index].id
这将返回该索引处文档的 id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.