[英]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.