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/pub.dartlang.org/geolocator-2.1.1/android/src/main/java/com/baseflow/flutter/plugin/geolocator/tasks/LastKnownLocationUsingLocationServicesTask.java:4: error: package android.support.annotation does not exist
import android.support.annotation.NonNull;
                                 ^
/flutter/.pub-cache/hosted/pub.dartlang.org/geolocator-2.1.1/android/src/main/java/com/baseflow/flutter/plugin/geolocator/tasks/LocationUpdatesUsingLocationServicesTask.java:5: error: package android.support.annotation does not exist
import android.support.annotation.NonNull;
                                 ^
  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;

      @override
      void initState() {
        // TODO: implement initState
        super.initState();
       getUserLocation();
      }
        @override
  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();
        print(currentLocation);
      }
    }

Any help is appreciated

1 Answers

1
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.