You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 13:25:22