Problem With the Background Location in Flutter SDK

Sanjog Satyarthi
4 min readSep 25, 2020

I was working on a project for an organization as a freelancer in which I had to track the worker’s location, I used Flutter SDK. The app needs to track real-time location while staying in the background as well, so I used google navigation in the foreground to navigate my worker to a location and to track location in the background I used a flutter plugin “background_location. This plugin was tracking location while the app stays in the foreground, but as soon as it goes in the background it stops tracking, I followed all the steps listed in the documentation of this plugin. I thought it’s just a plugin error so I tried other plugins like “background_locator. What I found while rectifying this problem is as listed below.

What was the problem?

The problem was the permission to capture location in the background, which lacked in background_location plugin.

Android permission documentation.

In order to protect their user’s privacy android doesn’t allow accessing private data like contacts, location, etc. without user consent. In order to access the data, a prompt needs to be given to the user asking them whether they want the app to access their data or not, this is known as Dangerous permissions.

These permissions need to be declared in the AndroidManifest.xml (app/src/main),

What I found while rectifying this problem was everything that needs permission needs to be declared in the AndroidManifest.xml file.

What was missing was ACCESS_BACKGROUNG_LOCATION declaration in the Manifest file permission which was not allowing the app to access location while staying in the background.

Location Permission

Android has 2 levels of Location permission :

  1. Foreground Location
  2. Background Location

In most of the cases, while using location in Flutter, we usually take location permission which allows us to access location only when it stays in the foreground. Like in background_location there is an option to get location permission from the user but only while app stays in the foreground. The major problem with this package is that it doesn’t provide any details about the background permissions.

background_locator(another plugin which I used) did mention this background permission but it is complicated to use and also it didn’t provide any permission option included in the plugin. We can use MethodInvoke or plugins to prompt for user permission to overcome this problem.

But there is a catch while accessing location permission, up to Android 9(API Level 28) the user is asked to either “Allow” or “Deny” the access to the location which includes background location too (if declared in Manifest file), in Android 10 (API Level 29) there are two options in the permission prompt i.e. “allow all the time” or “while the app is in use”. “Allow all the time” option allows background location(if declared in Manifest file) but in Android 11 (API Level 30) android has changed options to “while in use” or “this time”. In order to allow access to background location, the user needs to go to the settings and select “all the time” option. You can refer to the android documentation.

You can see the difference in the permission window from Android 9-10-11

Solution

A common solution is to ask directly to access location “all the time” in the app, instead of asking just for location access. By doing so, one can handle different API Level requirements in one go.

You can either use Method channel or flutter plugin. I have used a flutter plugin named permission_handler. And asked directly for locationAlways permission.

var permission = await Permission.locationAlways.isGranted;
if(!permission){
var t = await Permission.locationAlways.request();
}

You can declare this code at the topmost level of your widget tree to ensure that the app has the permission to access location in the background too.

You can see all the permissions like this in a dropdown, make sure you go through with all of these and declare.
Dropdown in the Manifest file displaying all the permissions.

Open your manifest file and go through all the permissions available in the dropdown, and declare them all.

Just due to permission issue you can see in the above how android restricted the use of location data in the background.

Conclusion

First and foremost make sure that you declare all your permission in the AndroidManifest.xml file if you miss any declaration then android won’t allow the use of that data in the release version of your app’s APK

Second, update yourself with the latest changes that android is doing with its upcoming versions and make sure that you implement them in your app’s code.

#team_flooters: Priyanshu Srivastava, Nimish Mehrotra, devashish Katiyar

--

--

Sanjog Satyarthi

A Cross-Platform app developer Flutter SDK | Firebase backend Services |Arduino Programmer