Reputation: 1
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
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:
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" />
Please keep in mind that:
<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 timeUpvotes: 0