1

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

primo
  • 1,340
  • 3
  • 12
  • 40
  • This is very old post but you can read this https://stackoverflow.com/questions/69487599/error-in-compilation-with-geolocator-pluggin – ViKi Vyas Feb 22 '22 at 08:51

1 Answers1

0

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.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
  • where is this file? – primo Apr 11 '19 at 10:00
  • If you are using Android studio, you can use shortcut `cmd + shift + o` and type the file name. – CopsOnRoad Apr 11 '19 at 10:01
  • You need top open this file in Android studio module, you are currently opening it in flutter module. See [how to open it in android module](https://stackoverflow.com/a/55548771/6618622). – CopsOnRoad Apr 11 '19 at 10:06
  • When you open `build.gradle` file it ask you to open this for editing in Android studio, when you do that, it will open up in a new window where you can open `LastKnownLocationUsingLocationServicesTask` using shortcut and delete imports. – CopsOnRoad Apr 11 '19 at 10:24
  • I'm sorry I never got a chance to use maps, so I can't help you with that. Seems like I know you because I have answered your question yesterday? Glad to see you accepting answers. – CopsOnRoad Apr 11 '19 at 10:45
  • 1
    yup. That's the reason I am asking you .You helped me out yesterday also – primo Apr 11 '19 at 10:47