I want to create a service that checks the user’s current position, and if the distance exceeds 100 meters, I update the user’s position in the Firestore database. I want this service to run in all cases: foreground, background, and even when the app is killed. Please suggest a solution.
I am new to Flutter, and I want you to help me implement this feature.
2
Answers
You can use the geolocator plugin to get the location of the user. For location tracking to work properly, make sure to request the necessary permissions from the user. You need to add the required permissions in your AndroidManifest.xml and Info.plist files for Android and iOS, respectively.
This plugin also provides services in background.
You can use workmanager for this, it will schedule tasks that will run in the background, so periodically you can check the user’s position and send it to Firebase.
You can do this using directly in native (Ref. Android, iOS) or use a package like Flutter Workmanager which will do this for you, it’s very simple to use, see an example below:
First you create the task to run
Then you’ll create a callback method that will run the code you want in the background as the task is called.
In your callback method you can even filter by conditions and have different treatments for different tasks.
If you are going to use this package, I recommend reading the original documentation and the usage example in the original repository.