Madhavam Shahi
Madhavam Shahi

Reputation: 1192

Update location in background?

I want to update user's location in background.

To perform tasks in background, i used this package, workmanager. Link to that: https://pub.dev/packages/workmanager

But i can not update the location, it seems like it can't work with async code?

here is my code,

void callbackDispatcher() {
  Workmanager.executeTask((task, inputData) async {
    await _HomeScreenState().updateUserLoc();
    print('it is working?');

    return Future.value(true);
  });
}
//Inside my stateFul Widget,



 void initState() {
    super.initState();
  
    
    Workmanager.initialize(
      callbackDispatcher,
      isInDebugMode: true,
    );

    Workmanager.registerPeriodicTask(
      "1",
      fetchBackground,
      frequency: Duration(minutes: 15),
    );
  }


updateUserLoc() async {
    print("executi9ng the updateUserLocccccc");
    await getusersLocation();
    print("executi9ng the updateUserLoc");
    GeoFirePoint point = geo.point(latitude: lat, longitude: long);
    _firestore.collection('locations').document(widget.userid).setData(
      {'position': point.data},
      merge: true,
    );
  }

Is there any other way of updating the users's location in background?

Upvotes: 2

Views: 329

Answers (1)

Szepetry
Szepetry

Reputation: 48

You have to call the call the code inside the updateUserLoc() directly inside the callbackDispatcher. Just make the callbackDispatcher async function.

Upvotes: 1

Related Questions