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

Flutter_map中Marker添加GestureDetector报错:缺少child及builder未定义

问题解决:flutter_map Marker添加GestureDetector报错

报错原因分析

  1. builder参数未定义:flutter_map的Marker组件并没有builder这个参数,标记的内容需要通过child参数传入。
  2. GestureDetector语法错误:代码中onTap回调后的逗号位置错误,导致child没有被正确识别为GestureDetector的必填参数。

修正后的代码

MarkerLayer(
  markers: [
    Marker(
      point: LatLng(-22.3285, -49.0523),
      width: 65,
      height: 50,
      // 替换builder为child
      child: GestureDetector(
        onTap: () {
          // 这里添加点击逻辑
        },
        // 修正逗号位置,将child作为GestureDetector的属性
        child: Container(
          padding: EdgeInsets.symmetric(horizontal: 4, vertical: 2),
          decoration: BoxDecoration(
            color: Colors.black.withOpacity(0.5),
            borderRadius: BorderRadius.circular(5),
          ),
          child: Text(
            'BLOCOS C, D & E',
            style: TextStyle(
              color: Colors.white,
              fontWeight: FontWeight.bold,
            ),
            textAlign: TextAlign.center,
          ),
        ),
      ),
    ),
  ],
),

关键修改点

  • 将Marker的builder替换为官方支持的child参数。
  • 调整GestureDetector中onTap后的逗号位置,确保child被正确传递给GestureDetector,满足其必填参数要求。

内容的提问来源于stack exchange,提问作者Raphael Michelassi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 23:14:53