Home > Blockchain >  denied permissions to access the device's location
denied permissions to access the device's location

Time:12-22

Position? position;
List<Placemark>? placeMarks;

getCurrentLocation() async
  {
    Position newPosition = await Geolocator.getCurrentPosition(
      desiredAccuracy: LocationAccuracy.high, //exact location use high
    );
    position = newPosition; //get longitude and latitude
    placeMarks = await placemarkFromCoordinates(
        position!.latitude,
        position!.longitude,
    );
    Placemark pMark = placeMarks![0];
    String completeAddress = '${pMark.subThoroughfare} ${pMark.thoroughfare}, '
        '${pMark.subLocality} ${pMark.locality}, ${pMark.subAdministrativeArea}, '
        '${pMark.administrativeArea} ${pMark.postalCode}, ${pMark.country}'; 
    location.text = completeAddress;
  }

child: ElevatedButton.icon(
                          onPressed: (){
                            getCurrentLocation();
                          },
                          icon: const Icon(
                            Icons.location_on,

i don't have error in my android studio but when i click button to get current location, it didn't appear my current location.

CodePudding user response:

You have to also request the location permission from the user. The permission_handler package allows you to use the system dialog to ask for permission:

if (await Permission.locationWhenInUse.request().isGranted) {
  // Either the permission was already granted before or the user just granted it.
}

If permission was already granted, it won't ask the user again but will return true. It also allows you to check if location is turned on:

if (await Permission.locationWhenInUse.serviceStatus.isEnabled) {
  // Use location.
}

You have to request the locationWhenInUse permission before asking for locationAlways.

(All examples adapted from the package README.)

CodePudding user response:

Use permission_handler Package, There are a number of Permissions. You can get a Permission's status, which is either granted, denied, restricted or permanentlyDenied.

var status = await Permission.camera.status;
if (status.isDenied) {
  // We didn't ask for permission yet or the permission has been denied before but not permanently.
}

// You can can also directly ask the permission about its status.
if (await Permission.location.isRestricted) {
  // The OS restricts access, for example because of parental controls.
}

Call request() on a Permission to request it. If it has already been granted before, nothing happens. request() returns the new status of the Permission.

if (await Permission.location.request().isGranted) {
  // Either the permission was already granted before or the user just granted it.
}

// You can request multiple permissions at once.
Map<Permission, PermissionStatus> statuses = await [
  Permission.location,
  Permission.storage,
].request();
print(statuses[Permission.location]);
  • Related