设备上的定位服务被禁用 [Flutter geolocator v6.1.4]

The location service on the device is disabled [Flutter geolocator v6.1.4]

我在尝试获取当前位置时总是收到该消息。我是 运行 我在真实设备上的应用程序。我遵循了这里的文档说明 geolocator plugin。起初它获取位置,然后在退出应用程序后再次启动它,它说

The location service is on the device is disabled

即使我已经启用了定位服务,为什么我仍会收到该错误消息?

class LocationController extends GetxController{

   var position = Position().obs;
   var lat = 0.00.obs;
   var long = 0.00.obs;
 Future getLocation() async {
   try{

       position.value = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);

       lat.value = position.value.latitude;
       long.value = position.value.longitude;
     } catch (e){
       print(e);
     }
  }
}

打电话...

locationController.getLocation();

下载并导入插件

geolocator and geocoding

import 'package:geocoding/geocoding.dart';
import 'package:geolocator/geolocator.dart';

在你的 AndroidManifest 添加权限

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

初始化变量

Position _currentPosition;
String _currentAddress = "";

当前位置函数

_getCurrentLocation() async {

    await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
    .then((Position position) {
  setState(() {
   //Pass the lat and long to the function
    _getAddressFromLatLng(position.latitude, position.longitude);

  });
}).catchError((e) {
  print(e);
});
  }

_getAddressFromLatLng

 _getAddressFromLatLng(double latitude, double longitude) async {
    try {
      List<Placemark> p = await placemarkFromCoordinates(latitude, longitude);

      Placemark place = p[0];

      setState(() {
        _currentAddress = "${place.locality}";
        print(_currentAddress);       
    
      });
    } catch (e) {
      print(e);
    }
  }

如果上述问题只发生 运行 flutter clean,卸载应用程序然后重新启动 phone 和 运行 项目