Flutter Windows桌面应用如何用firedart提取FireStore文档ID列表?
提取Firestore文档ID生成列表(firedart包)
我正在使用Flutter开发Windows桌面应用,因官方FireStore包不支持Windows,故采用firedart包。目前已能获取FireStore中'users'集合的文档数据,但希望仅提取文档ID并生成列表。
现有代码
FirebaseAuth.initialize(apiKey, VolatileStore()); Firestore.initialize(projectId); CollectionReference stream = Firestore.instance.collection('users'); final data = await stream.get(); print(data);
当前输出示例
[/users/1dfU7emalRX89z5zlfX0AQLqehq1 {url: '', name: '', height:'' , weight: '',}, /users/JF6PMb2q9Igsb56jgPRs1DGzJ0d2{url: '', name: '', height:'' , weight: '',}]
解决方案
firedart中调用get()返回的QuerySnapshot对象包含docs属性,这是一个List<Document>集合,每个Document实例都有id字段直接对应文档ID。你可以通过遍历这个列表提取ID:
修改后的代码:
FirebaseAuth.initialize(apiKey, VolatileStore()); Firestore.initialize(projectId); CollectionReference usersCollection = Firestore.instance.collection('users'); final QuerySnapshot snapshot = await usersCollection.get(); // 提取所有文档ID到列表 List<String> documentIds = snapshot.docs.map((document) => document.id).toList(); print(documentIds);
执行后会输出纯ID列表,比如:
[1dfU7emalRX89z5zlfX0AQLqehq1, JF6PMb2q9Igsb56jgPRs1DGzJ0d2]
内容的提问来源于stack exchange,提问作者taha khamis
相关产品推荐
相关产品推荐

