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

Flutter使用OpenStreetMap报错:layers命名参数不存在如何修复?

修复flutter_map 6.0.0的layers参数错误

这个错误是因为flutter_map 6.0.0版本做了重大API更新,移除了原有的layers参数,同时TileLayerOptions组件也被替换为TileLayer。按以下步骤修改代码即可解决:

  • 移除FlutterMap组件中的layers参数,改用children属性放置图层组件
  • 将TileLayerOptions替换为TileLayer组件,参数用法基本一致

修复后的完整代码:

import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong2/latlong.dart';

class MapPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('OpenStreetMaps'),
      ),
      body: FlutterMap(
        options: const MapOptions(
          center: LatLng(51.5, -0.09),
          zoom: 13.0,
        ),
        // 用children替代原来的layers
        children: [
          TileLayer(
            urlTemplate: 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
            subdomains: const ['a', 'b', 'c'],
          ),
        ],
      ),
    );
  }
}

pubspec.yaml的依赖配置无需修改,保持现有版本即可:

flutter_map: ^6.0.0
latlong2: ^0.9.0

内容的提问来源于stack exchange,提问作者Asa Marsal

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 23:26:07