Ashutosh Deshmukh
Ashutosh Deshmukh

Reputation: 1

How to Implement Background Services in a Flutter App to Fetch Location While the App is Killed?

I am working on a Flutter application that needs to continuously fetch the user's lat nd long, even when the app is killed or in the background. I understand that for this functionality, I need to create a bridge between Kotlin and Dart.

Here are my main questions:

Running Background Services: What is the recommended approach for implementing a background service in Kotlin that will fetch the location periodically? I want to ensure that the service runs even when the app is not active.

Fetching Location: Once the background service is set up, how can I fetch the user's lat nd long from the Kotlin service and pass this data back to the Dart side? Is there a specific way to handle this, and what permissions should I manage in the AndroidManifest.xml?

Help me to get rid of this issue...!

Create kotlin file which fetches users lat and long but not able to create bridge between kotlin and dart.

Upvotes: 0

Views: 259

Answers (1)

Maciej Bastian
Maciej Bastian

Reputation: 311

Personally I rely fully on native when using background/foreground location fetching because Flutter runs in Kotlin's UI thread

I would create a background service that fetches the user's location and then passes it to some repository that saves in the storage. Later, when the user open the Flutter app, flutter reads that storage and accesses the data. In this kind of approach there is no need to pass arguments between kotlin and flutter all the time because in foreground flutter can periodically check for changes in storage. Specific approach on how to store location data is up to you though.

As per background service, there is a lot of work to be done here. You need to take care of two things:

  1. GET LOCATION PERMISSION: You need to add permissions I listed below to AndroidManifest.xml and request those permissions in your flutter app before starting a background service (refer to Permission handler package). please note that you have to request background location in two steps: first ask for general location permission and then take user to settings to explicitly allow to use location "all the time"
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
  1. START BACKGROUND SERVICE AND STORE LOCATION: create a method channel like described here and inside this method channel invoke a method/repository that starts a background service which requests the location and then stores it into storage. Full documentation here

Please keep in mind that:

  1. You are allowed to request the location in background only every 15 minutes
  2. User can revoke "use all the time" location permission at any time, which will break your background use
  3. If you need frequent location updates, you should consider using a Foreground service which will require you to declare <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> and <uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" /> permissions in your manifest, but it will not work when your app is terminated and user can stop your service at any time
  4. You need to implement it on kotlin side and use flutter for UI in this case, as trying to rely on Flutter for background services can be cumbersome and can potentially cause a lot of errors
  5. Different Android versions have different approach on background location (and location in general), please check android docs for more info

Upvotes: 0

Related Questions