0

In my flutter application I want to give location access.

in my pubspec.yaml I have

  google_maps_flutter: ^2.0.6
  geolocator: ^8.2.1
  geocoding: ^2.0.0  
  permission_handler: ^9.2.0

I am using Flutter 2.10.5

I have following code to check location access;

Future<void> _checkMapAuthentication() async {
   
    await PermissionHandler.Permission.location.serviceStatus
        .then((status) async {
      print("PermissionHandler.ServiceStatus -> $status");

      //IF GPS(Location) Service is disabled, then request to enable it.
      if (status == PermissionHandler.ServiceStatus.disabled) {
        Location location = new Location();
        bool _serviceEnabled;

        _serviceEnabled = await location.serviceEnabled();

        if (!_serviceEnabled) {
          await location.requestService().then((locationServiceStatus) async {
            if (!locationServiceStatus) {
              print("location service not enabled ");
            } else {
              //If Location Service is ON, then show the Message for Using Service While in use.
              await _requestWhileInUsePermission();
            }
          });
        } else {
          await _requestWhileInUsePermission();
        }
      } else {
        await PermissionHandler.Permission.locationWhenInUse.status
            .then((status) async {
          print(
              "checking PermissionHandler.Permission.locationWhenInUse -> $status");
          if (status == PermissionHandler.PermissionStatus.granted) {
            print("have while in use access");
          } else {
            print("request PermissionHandler.Permission.locationWhenInUse");
            await PermissionHandler.Permission.locationWhenInUse
                .request()
                .then((locationWhenInUseStatus) async {
              if (locationWhenInUseStatus.isDenied) {
                print("while in use access denied");
              } else {
                print("got while in use access");
              }
            });
          }
        });
      }
    });
  }

  Future<void> _requestWhileInUsePermission() async {
    await PermissionHandler.Permission.locationWhenInUse
        .request()
        .then((locationWhenInUseStatus) async {
      if (locationWhenInUseStatus.isDenied) {
        print("while in use access denied");
      } else {
        print("have while in use access");
      }
    });
  }

In my manifest file I have following settings

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />

When I run the application for the first time. It goes to line

await PermissionHandler.Permission.locationWhenInUse.request()

And stops there, I don't get any print statements I have there.

If I run the application again without deleting it from the simulator I can see the output

"have while in use access"

So the request was granted in first time but didn't go to ".then" part

When I run the application first time I do get following in the console

Accessing hidden method Landroid/content/ContextWrapper;->getDisplay()Landroid/view/Display; (light greylist, linking)

Can anyone provide some advice on this?

Janaka
  • 2,505
  • 4
  • 33
  • 57

1 Answers1

0

Please checkout this answer: How to identify what is accessing hidden methods

this will resolved your problem otherwise watch this video : 1) https://www.youtube.com/watch?v=Zz5hMvgiWmY 2) https://www.youtube.com/watch?v=2p4snh1-JQM

amit.flutter
  • 883
  • 9
  • 27