Current location using geolocator

Asked by At

I am trying to get current location using geolocator sdk of flutter.But I am getting this error.

This is the stack trace

/flutter/.pub-cache/hosted/ error: package does not exist
/flutter/.pub-cache/hosted/ error: package does not exist
  symbol: class NonNull
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
4 errors

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':geolocator:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

I am trying to get current location here like this

    class _MapActivityState extends State<MapActivity> {

      Position currentLocation;

      void initState() {
        // TODO: implement initState
  Widget build(BuildContext context) {
    return Container(
      child: MapboxMap(
        initialCameraPosition: CameraPosition(target: _center, zoom: 15),
        onMapCreated: _onMapCreated,
        myLocationEnabled: true,
      Future<Position> locateUser() async {
        return Geolocator()
            .getCurrentPosition(desiredAccuracy: LocationAccuracy.high);

      getUserLocation() async {
        currentLocation = await locateUser();

Any help is appreciated

1 Answers

CopsOnRoad On Best Solutions

Open LastKnownLocationUsingLocationServicesTask file, delete the import statement which is causing this error, again import it using option + enter, I have seen this issue while using geolocator.

Similarly, open LocationUpdatesUsingLocationServicesTask file and delete import (with errors) and reimport it.