简体   繁体   English

flutter 未处理的异常:类型“LatLng”不是类型“LatLng”的子类型

[英]flutter Unhandled Exception: type 'LatLng' is not a subtype of type 'LatLng'

I am using this plugins platform_maps_flutter & google_places_for_flutter to display google map in android device and apple map in iOS device.我正在使用此插件platform_maps_flutter & google_places_for_flutter在 android 设备中显示 google map,在 iOS 设备中显示 apple map。 This plugin google_places_for_flutter is used to get LatLng whatever i type on textfield.这个插件google_places_for_flutter用于获取LatLng无论我在文本字段上键入什么。 Problem is when i type something and click on any place suggestions then google map or apple map doesn't move to that position instead it showing me this error问题是当我输入一些东西并点击任何地方建议然后谷歌 map 或苹果 map 不会移动到那个 position 而是它向我显示这个错误

[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'LatLng' is not a subtype of type 'LatLng' where
E/flutter (31603):   LatLng is from package:google_maps_flutter_platform_interface/src/types/location.dart
E/flutter (31603):   LatLng is from package:platform_maps_flutter/platform_maps_flutter.dart
E/flutter (31603): 
E/flutter (31603): #0      _GoogleMapDistrictPageState.build.<anonymous closure> (package:tuxedo/MainScreens/GoogleMapDistrictPage.dart:152:50)

GoogleMapDistrictPage.dart GoogleMapDistrictPage.dart

class GoogleMapDistrictPage extends StatefulWidget {
  const GoogleMapDistrictPage({Key? key}) : super(key: key);

  @override
  _GoogleMapDistrictPageState createState() => _GoogleMapDistrictPageState();
}

class _GoogleMapDistrictPageState extends State<GoogleMapDistrictPage> {
  late PlatformMapController controller;
  late LatLng currentLatLng;
  late Position currentLocation;
  TextEditingController names = TextEditingController();

  Future<LatLng> get() async {
    Position? position = await Geolocator.getLastKnownPosition();
    return LatLng(position!.latitude, position.longitude);
  }

  Future<Position?> locateUser() async {
    return Geolocator.getLastKnownPosition();
  }

  getUserLocation() async {
    currentLocation = (await locateUser())!;
    setState(() {
      currentLatLng =
          LatLng(currentLocation.latitude, currentLocation.longitude);
    });
  }

  @override
  void initState() {
    getUserLocation();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MediaQuery(
      data: MediaQueryData.fromWindow(WidgetsBinding.instance!.window)
          .copyWith(boldText: false, textScaleFactor: 1.0),
      child: Scaffold(
          appBar: AppBar(
            leading: GestureDetector(
                onTap: () {
                  Navigator.pop(context);
                },
                child: const Icon(Icons.arrow_back_ios)),
            automaticallyImplyLeading: false,
            elevation: 0,
            backgroundColor: TuxedoColor.redColor,
          ),
          body: Stack(
            children: [
              FutureBuilder<LatLng>(
                future: get(),
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    final locationModel = snapshot.data!;
                    return PlatformMap(
                        onMapCreated:
                            (PlatformMapController platformMapController) {
                          setState(() {
                            controller = platformMapController;
                          });
                        },
                        mapType: MapType.normal,
                        initialCameraPosition:
                            CameraPosition(target: locationModel, zoom: 15.0),
                        myLocationEnabled: true,
                        myLocationButtonEnabled: true,
                        padding: EdgeInsets.only(
                          top: MediaQuery.of(context).size.height * 0.10,
                        ),
                        onCameraMove: (position) {
                          currentLatLng = position.target;
                        });
                  }
                  return PlatformCircularProgressIndicator();
                },
              ),
              const Align(
                alignment: Alignment.center,
                child: Icon(
                  Icons.place,
                  color: Colors.red,
                  size: 50.0,
                ),
              ),
              Positioned(
                  top: 5,
                  left: 5,
                  right: 5,
                  child: SearchGooglePlacesWidget(
                    hasClearButton: true,
                    placeType: PlaceType.address,
                    placeholder: 'Enter the location'.tr(),
                    apiKey: 'API_KEY',
                    onSelected: (Place place) async {
                      final geolocation = await place.geolocation;
                      controller.animateCamera(
                          CameraUpdate.newLatLng(geolocation?.coordinates)); // On this line error
                      controller.animateCamera(
                          CameraUpdate.newLatLngBounds(geolocation?.bounds, 0));
                    },
                    onSearch: (Place place) {},
                  )),
            ],
          )),
    );
  }
}

Since they're from two different packages.因为它们来自两个不同的包。 You need to convert it.你需要转换它。 I'm assuming this is the line that's causing an error.我假设这是导致错误的行。

import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart' hide LatLng;
onSelected: (Place place) async {
   final geolocation = await place.geolocation;
   // Convert to proper package's LatLng
   final latLng = LatLng(geolocation?.coordinates.latitude,geolocation?.coordinates.longitude);
   final bounds =  LatLngBounds(southwest: latLng, northeast: latLng); 

      controller.animateCamera(
         CameraUpdate.newLatLng(latLng)); // On this line error

      controller.animateCamera(
         CameraUpdate.newLatLngBounds(bounds, 0));
},

You have to ignore that class in one of the two imports.您必须忽略两个导入之一中的 class。 For example:例如:

import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart' 
hide LatLng;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “字符串”类型不是“列表”类型的子类型<latlng> ' 我如何将闲置的 flutter 字符串转换为 LatLng 以使用 Polygon</latlng> - type 'String' is not a subtype of type 'List<LatLng>' how do i convert the fallowing flutter string to LatLng for the use of Polygon Flutter 中关于 LATLNG 包和 LatLng 类型的问题 - A question regarding LATLNG package & LatLng type in Flutter Flutter - 未处理的异常:未处理的错误类型“int”不是“日期时间”类型的子类型? - Flutter - Unhandled Exception: Unhandled error type 'int' is not a subtype of type 'DateTime?' 未处理的异常:输入“未来”<dynamic> ' 不是类型 'String' 的子类型:Flutter 异常</dynamic> - Unhandled Exception: type 'Future<dynamic>' is not a subtype of type 'String' : Flutter Exception 未处理的异常:类型“int”不是 Flutter 应用程序中“字符串”类型的子类型 - Unhandled Exception: type 'int' is not a subtype of type 'String' in Flutter app Flutter:未处理的异常:类型“QuerySnapshot”不是“Future”类型的子类型<dynamic> ? - Flutter: Unhandled Exception: type 'QuerySnapshot' is not a subtype of type 'Future<dynamic>?' 未处理的异常:类型 '_InternalLinkedHashMap<string, dynamic> ' 不是“ReferralPolicyResponse”类型的子类型? Flutter</string,> - Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'ReferralPolicyResponse?' Flutter Flutter:未处理的异常:类型“_ByteDataView”不是“Uint8List”类型的子类型 - Flutter: Unhandled Exception: type '_ByteDataView' is not a subtype of type 'Uint8List' Flutter '未处理的异常:类型'列表<dynamic> ' 不是类型 'List 的子类型<class> '</class></dynamic> - Flutter 'Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<class>' Flutter:未处理的异常:键入“列表”<dynamic> ' 不是类型 'List 的子类型<string> '</string></dynamic> - Flutter:Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<String>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM