gpt4 book ai didi

firebase - 我如何从 flutter 的Firestore中检索每个文档的文档ID

转载 作者:行者123 更新时间:2023-12-03 04:20:27 25 4
gpt4 key购买 nike

在这里,我有从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(),
);
}
},
),
这是我的吸气功能
     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。

关于firebase - 我如何从 flutter 的Firestore中检索每个文档的文档ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62967547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com