Home > Mobile >  how can i send data to server interval times
how can i send data to server interval times

Time:07-13

  • it's my code
class TrackingManager {
  Location locate = new Location();
  Future<Map<String, double>> trackOn() async {
    LocationData getlocationdata = await locate.getLocation();
    Map<String, double> locationdata = {};
    locationdata['latitude'] = getlocationdata.latitude ?? 0;
    locationdata['longitude'] = getlocationdata.longitude ?? 0;
    return locationdata;
  }
}
  • if controller(user) call TrackingManager and initialize object, and use trackOn so Stores the location information of the current user.
class LocateApiRequest {
  final _networkManager = NetworkManager();
  final _trackingManager = TrackingManager();
  Future transmit(String accessToken) async {
    final locationdata = await _trackingManager.trackOn();
    final response = await _networkManager
        .send(ApiRequestFactory.transmit(locationdata, accessToken));

    final statusCode = response.statusCode;
    if (statusCode == 200) {
      final responseBody = jsonDecode(response.body);
      return responseBody;
    }
    throw statusCode;
  }
}

and Send the data to the server by requesting a post

But this action is carried out only once. Starting with when a user clicks a specific button, I want to do this on the server with a regular time (background, foreground status is not important)

how can i solve it?I've thought about using a repeat statement, but this doesn't seem reasonable

CodePudding user response:

using location callback

https://pub.dev/packages/location

location.onLocationChanged.listen((LocationData currentLocation) {
  // call network api 

  // Use current location
});
  • Related