Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Location Permission Status – always getting as denied in Flutter

I have created below method to check the location permission status in my flutter application:

Future<void> checkLocationPermission(
  BuildContext context, UserCredential userCredential) async {
locationPermissionStatus = await permission_status.Permission.location
    .request()
    .then((value) async {
  if (locationPermissionStatus.isGranted) {
    displayToast("granted");
    await getCurrentLocation(context, userCredential);
  } else {
    displayToast("not granted");
  }
  return locationPermissionStatus;
});
 
}

Where, PermissionStatus is declared as below:

 permission_status.PermissionStatus locationPermissionStatus =
          permission_status.PermissionStatus.denied;

Imports as below:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import 'package:location/location.dart';
import 'package:permission_handler/permission_handler.dart' as permission_status;

Used below plugins:

permission_handler: ^10.2.0
location: ^4.4.0

But In App, When Location Permission pops up, I am tapping on option "While Using the App"

But I am always getting the toast display as "not granted"

What might be the issue? Thanks in Advance.

EDIT:
Already added below permissions for Android:

  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.mypackage">
        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.CAMERA"/>
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    </manifest>

>Solution :

Adding to the @Romil Mavani answer, you first have to add permissions in AndroidManifest:

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

But it also seems that you are using async/await with the combination of then which is very error-prone. You should either use one or the other.

Future<void> checkLocationPermission(
    BuildContext context, UserCredential userCredential) async {
  locationPermissionStatus =
      await permission_status.Permission.location.request();
  if (locationPermissionStatus.isGranted) {
    displayToast("granted");
    await getCurrentLocation(context, userCredential);
  } else {
    displayToast("not granted");
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading