Unhandled Exception: User denied permissions to access the device‘s location.

在写android app时,有的时候遇到这样的错误:未处理的异常:用户拒绝访问设备位置的权限。

即使加上了官网讲述的:下面两行代码任意一行,也不行。


Unhandled Exception: User denied permissions to access the device‘s location._第1张图片

 之前写的:

void getLocation() async {
    Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    print(position);
}

 改为:

void getLocation() async {
    LocationPermission permission = await Geolocator.requestPermission();
    Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    print(position);
}

也就是多加一行:

LocationPermission permission = await Geolocator.requestPermission();

你可能感兴趣的:(flutter,getLocation,Dart)