Flutterlocation: requestPermission call on location object does not return on first call

Created on 9 Apr 2020  路  18Comments  路  Source: Lyokone/flutterlocation

_serviceEnabled = await location.serviceEnabled();
    if (!_serviceEnabled) {     
      _serviceEnabled = await location.requestService();     
      if (!_serviceEnabled) {
        return null;
      }
    }    
    _permissionGranted = await location.hasPermission();
    if (_permissionGranted == PermissionStatus.denied) {         
      _permissionGranted = await location.requestPermission();//this is where execution goes to die     
      if (_permissionGranted != PermissionStatus.granted) {        
        return null;
      }

Describe the bug
I have the following code where I try to get location of the user. When this code is invoke for the very first time, the method requestPermission is where I get prompted on my phone for permission, when I press allowed, the method should return and execution continue.

It is worth noting that, if I do a hot reload during this perpetual wait, the request permission comes as granted (on this line _permissionGranted = await location.hasPermission();) without prompt.

Expected behavior
When I allow permission on prompt, the program execution continue and the method requestMethod return a Future of PermissionStatus.

Tested on:

  • Android, API Level 23, real device (Huawei)
  • Not Tested On iOS

Following is my flutter doctor

Doctor summary (to see all details, run flutter doctor -v):
[鈭歖 Flutter (Channel stable, v1.12.13+hotfix.8, on Microsoft Windows [Version 10.0.18362.535], locale en-US)

[鈭歖 Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[鈭歖 Android Studio (version 3.6)
[鈭歖 IntelliJ IDEA Community Edition (version 2018.1)
[!] IntelliJ IDEA Ultimate Edition (version 2016.2)
X Flutter plugin not installed; this adds Flutter specific functionality.
X Dart plugin not installed; this adds Dart specific functionality.
X This install is older than the minimum recommended version of 2017.1.0.
[鈭歖 VS Code, 32-bit edition (version 1.43.2)
[鈭歖 Connected device (1 available)

! Doctor found issues in 1 category.

bug inactive

Most helpful comment

I also ran on API 28 PIXEL to same result.

I am using location: ^3.0.2

(Sorry about not putting the code in a code block above, for some reason, in a code block all the lines of code when being turned into one line)

All 18 comments

I also ran on API 28 PIXEL to same result.

I am using location: ^3.0.2

(Sorry about not putting the code in a code block above, for some reason, in a code block all the lines of code when being turned into one line)

I am also facing same issue...

Hey ! thanks for the report, I'll check that !

@Lyokone what is the status of that bug means after giving permission they ask for to turn gps by pooping dialog box like in android but then return nothing and code is break

Hey ! thanks for the report, I'll check that !

Hi Lykone, Turn on Gps from dialog doesn't return any status. Could you please solve this as soon as possible

A temporary work around I am using (until this gets resolved) is I have placed requestPermission in a loop that runs a maximum of three times. So far it only fails to get permission the first time, and the second time it gets permission, which serves my purpose (in my use case the user doesn't have to wait to get permission since I am collecting it for later use)

Hi @sajidbarcha

I am not getting any call back for this code and also nothing can be executed after this line.
bool serviceRequestedResult = await location.requestService();

Could you please post any temporary solution for this

you have to make a call like recurssion

Hi @sajidbarcha

I am not getting any call back for this code and also nothing can be executed after this line.
bool serviceRequestedResult = await location.requestService();

Could you please post any temporary solution for this

Hope this helps. I don't await, I let location resolve itself in the background, and once I get the location, I store in the provider.

location.requestPermission().then((value) {
        //this is the bug, where value comes asynchronously as a null...
        if (value == null) {
          location.requestPermission().then((value2) {
            //so we request again, this time it gets the permission status correctly.
            //a bit hackey, but solves my probem for now
            _permissionGranted = value2;
          });
        }
        _permissionGranted = value;
        return null;
      });

Hello,

Plugin is already using flutter embedding v2, so we have to update our Android project, https://github.com/flutter/flutter/wiki/Upgrading-pre-1.12-Android-projects or recreate the project using flutter create .

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

I am also experiencing this issue . Any progress?

still facing this issue.. any advice?

Any progress on this please?

Is this library maintained any more?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Not fixed yet

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

giudon010 picture giudon010  路  3Comments

fasteater picture fasteater  路  11Comments

zanecidan picture zanecidan  路  6Comments

zeienko-vitalii picture zeienko-vitalii  路  7Comments

Dorbmon picture Dorbmon  路  8Comments