在写android app时,有的时候遇到这样的错误:未处理的异常:用户拒绝访问设备位置的权限。
即使加上了官网讲述的:下面两行代码任意一行,也不行。
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
之前写的:
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();