flutterdartgoogle-mapsmapsapple-maps

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. This plugin google_places_for_flutter is used to get LatLng whatever i type on textfield. 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

[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

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) {},
                  )),
            ],
          )),
    );
  }
}

Solution

  • 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));
    },