flutter 参数类型'LatLng'无法指派给参数类型'LatLng?'

6rqinv9w  于 2022-12-14  发布在  Flutter
关注(0)|答案(3)|浏览(334)

当我在MapOptions-〉Center中使用flutter_map时,当输入lat和lng时,我得到一个错误。参数类型'LatLng'不能分配给参数类型'LatLng?'。

class MapsView extends HookConsumerWidget {
  MapsView({Key? key}) : super(key: key);
   final MapController _mapController=MapController();

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return FlutterMap(
      options: MapOptions(
       center: LatLng(51.509364, -0.128928),
        zoom: 9.2,
      ),
      layers: [
        TileLayerOptions(
          urlTemplate: "https://tile.openstreetmap.org/{z}/{x}/{y}.png",
          userAgentPackageName: 'com.example.app',
        ),
      ],
      nonRotatedChildren: [
        AttributionWidget.defaultWidget(
          source: 'OpenStreetMap contributors',
          onSourceTapped: null,
        ),
      ],
    );
  }

  }

5lhxktic

5lhxktic1#

import 'package:latlong2/latlong.dart';
import 'package:flutter_map/flutter_map.dart';
 class MapsView extends HookConsumerWidget {
  MapsView({Key? key}) : super(key: key);
 final MapController _mapController=MapController();

 @override
  Widget build(BuildContext context, WidgetRef ref) {
return FlutterMap(
  options: MapOptions(
   center: LatLng(51.509364, -0.128928),
    zoom: 9.2,
  ),
  layers: [
    TileLayerOptions(
      urlTemplate: "https://tile.openstreetmap.org/{z}/{x}/{y}.png",
      userAgentPackageName: 'com.example.app',
    ),
  ],
  nonRotatedChildren: [
    AttributionWidget.defaultWidget(
      source: 'OpenStreetMap contributors',
      onSourceTapped: null,
    ),
  ],
);
}

 }

导入'软件包:latlong 2/latlong.dart

未在软件包中导出,因此您必须手动导入它才能在其中赋值x1c 0d1x

lvmkulzt

lvmkulzt2#

我觉得你需要的不是latLng2
第一个

zzzyeukh

zzzyeukh3#

如果您已经使用latlong: ^0.6.1或更高版本,则需要将导入从:

import "package:latlong/latlong.dart" as latLng;

至:

import 'package:latlong2/latlong.dart' as latLng;

一切都会好的。

相关问题