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

Flutter/Dart中如何在StreamBuilder内获取Firestore文档ID

问题:如何获取Firestore文档ID并传入buildNearbyCard?

我正在使用Firestore、geoflutterfire和cloudfirestore插件获取当前地理位置附近的条目,希望将文档ID传入buildNearbyCard,但不知道如何访问该ID,请问我遗漏了什么?

相关代码片段:

StreamBuilder部分:

StreamBuilder<List<DocumentSnapshot>>(
                      stream: stream,
                      builder: (context,
                          AsyncSnapshot<List<DocumentSnapshot>> snapshot) {
                        if (snapshot.data!.isNotEmpty) {
                          return SizedBox(
                            height: 140,
                            child: ListView.separated(
                                scrollDirection: Axis.horizontal,
                                // padding: const EdgeInsets.all(12),
                                itemCount: 5,
                                separatorBuilder: (context, index) {
                                  return const SizedBox(width: 15);
                                },
                                itemBuilder: (context, index) {
                                  DocumentSnapshot data =
                                      snapshot.data![index];
                                  GeoPoint documentLocation =
                                      data.get('position')['geopoint'];
                                  var distanceInMeters =
                                      Geolocator.distanceBetween(
                                          center.latitude,
                                          center.longitude,
                                          documentLocation.latitude,
                                          documentLocation.longitude);
                                  var distInMiles =
                                      (distanceInMeters * 0.00062137)
                                          .toStringAsFixed(1);
                                  return buildNearbyCard(
                                    index: index,
                                    titleName: '${data.get('name')}',
                                    distance: '${distInMiles}',
                                    cost: '${data.get('cost')}',
                                    );
                                }),
                          );
                        } else {

地理搜索部分:

var ref = _firestore.collection('locations');
    double radius = 40; //radius is set to 40kms
    String field = 'position';
    Stream<List<DocumentSnapshot>> stream = geo
        .collection(collectionRef: ref)
        .within(center: center, radius: radius, field: field);

解决方案

你不需要额外的查询或操作,DocumentSnapshot对象本身就自带id属性,直接访问即可获取对应的Firestore文档ID。

修改你的buildNearbyCard调用代码,添加文档ID参数:

return buildNearbyCard(
  index: index,
  titleName: '${data.get('name')}',
  distance: '${distInMiles}',
  cost: '${data.get('cost')}',
  documentId: data.id, // 直接获取文档ID
);

同时需要确保你的buildNearbyCard函数已经定义了documentId参数来接收这个值,比如:

Widget buildNearbyCard({
  required int index,
  required String titleName,
  required String distance,
  required String cost,
  required String documentId, // 添加这个参数
}) {
  // 你的卡片构建逻辑
}

geoflutterfire返回的DocumentSnapshot和普通Firestore查询返回的对象完全一致,包含了文档的所有元数据(包括ID),所以直接通过data.id就能拿到你需要的文档ID。


内容的提问来源于stack exchange,提问作者Pierre Louet

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 14:25:22