[英]Getting all documents uids from firestore and add them to a list .flutter
我在我的 firestore 项目中创建了一个名为 users 的新集合。 我试图获取该集合中所有自动生成的 id 的列表。 到目前为止我试过
late List<String> userID = [];
Future getID() async {
await FirebaseFirestore.instance.collection('users').get().then(
(snapshot) => snapshot.docs.forEach((document) {
userID.add(document.reference.id);
}),
);
}
但是每当我尝试访问列表中的 id 字符串时,它都会返回一个空列表
getID
是一种未来的方法。 获取数据需要一些时间。 获取数据后,您需要调用 setState 来更新 UI。 你不需要await
和.then
同时
尝试
Future getID() async {
FirebaseFirestore.instance.collection('users').get().then((snapshot) {
snapshot.docs.forEach((document) {
userID.add(document.reference.id);
});
setState(() {});
});
}
将 FutureBuilder 用于未来方法会很棒。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.