2

I'm using Location flutter package to access the user's background location, first time any user opens the app it asks for permission, and when the user accepts it brings back this error in the console

Unhandled Exception: PlatformException(PERMISSION_DENIED_NEVER_ASK, Background location permission denied forever - please open app settings, null, null)

if the user closed the app and reopened it .. it works perfectly fine ( fetches location in both foreground and background) without even asking again for location permission.

Following the getting started guide in the package itself, here is how I added permission to my AndroidManifest.xml file :

  <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
  <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
4
  • Have you found solution for this issue? Commented Feb 13, 2022 at 20:07
  • @AhmedElsayed No, too bad I had to force restart the app so in the second time it runs the permission flow goes normally Commented Feb 14, 2022 at 9:08
  • 1
    Thank you! Have you tried geolocator? Does it have same problem? Commented Feb 14, 2022 at 10:18
  • 1
    ok, Geolocator has same issue but I've found solution and posted below. Commented Feb 16, 2022 at 14:26

2 Answers 2

5

I've been working on this issue and I discovered that the problem is when you use location.enableBackgroundMode() then choose always when you hit back first time it throws an exception and isBackgroundModeEnabled will be false too "despite the backgroundmode is enabled in system" and you have to restart the app to check if background mode is enabled correctly.

but, I've found that calling location.enableBackgroundMode() again is solving the issue too and it really doesn't ask for enabling background again but it somehow make isBackgroundModeEnabled return true value. Here's my fix code:

Future<bool> enableBackgroundMode() async {
bool _bgModeEnabled = await location.isBackgroundModeEnabled();
if (_bgModeEnabled) {
  return true;
} else {
  try {
    await location.enableBackgroundMode();
  } catch (e) {
    debugPrint(e.toString());
  }
  try {
    _bgModeEnabled = await location.enableBackgroundMode();
  } catch (e) {
    debugPrint(e.toString());
  }
  print(_bgModeEnabled); //True!
  return _bgModeEnabled;
 }
}
Sign up to request clarification or add additional context in comments.

Comments

0

i have solved this error by an easy way, i opened the my app settings and go through permissions and i found that the permission fo locations is denied for all time, so i have changed to be allowed all time. and it worked again:)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.