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

When writing an android app, sometimes I encounter such an error: Unhandled exception: User denied permission to access device location.

Even if the official website is added: any of the following two lines of code will not work.

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

 Written before:

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

 Change to:

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

That is, add one more line:

LocationPermission permission = await Geolocator.requestPermission();

Guess you like

Origin blog.csdn.net/DongShanYuXiao/article/details/131942555