使用geolocator的当前位置

问题描述 投票:1回答:1

我正在尝试使用geolocator sdk of flutter获取当前位置。但是我收到此错误。

这是堆栈跟踪

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

我想在这里获取当前位置

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

任何帮助表示赞赏

flutter
1个回答
1
投票

打开LastKnownLocationUsingLocationServicesTask文件,删除导致此错误的import语句,再次使用option + enter导入它,我在使用geolocator时看到了这个问题。

同样,打开LocationUpdatesUsingLocationServicesTask文件并删除导入(有错误)并重新导入它。

© www.soinside.com 2019 - 2024. All rights reserved.