2

I'm use Pedometer plugin (https://pub.dev/packages/pedometer#-installing-tab-) on my Flutter Apps. Can someone give me an answer how to reset the step count value ? already tried using timer to reset the value to 0 but the value is still from the last step count.

Here are the code that i'm using for testing the pedometer:

Please kindly help me I'm new for this. Thanks

@override
  void initState() {
    super.initState();
    setUpPedometer();
  }

  void setUpPedometer() {
    Pedometer pedometer = new Pedometer();
    _subscription = pedometer.stepCountStream.listen(_onData,
        onError: _onError, onDone: _onDone, cancelOnError: true);
  }

  void _onData(stepCountValue) async {
    setState(() {
      _stepCountValue = "$stepCountValue";
      _step = stepCountValue;
    });

    var dist = _step;
    double y = (dist + .0);

    setState(() {
      _numerox =
          y;
    });

    var long3 = (_numerox);
    long3 = num.parse(y.toStringAsFixed(2));
    var long4 = (long3 / 10000);

    int decimals = 1;
    int fac = pow(10, decimals);
    double d = long4;
    d = (d * fac).round() / fac;
    print("d: $d");

    getDistanceRun(_numerox);

    setState(() {
      _convert = d;
      print(_convert);
    });
  }

  void reset() {
    setState(() {
      int stepCountValue = 0;
      stepCountValue = 0;
      _stepCountValue = "$stepCountValue";
    });
  }

  void _onDone() {}

  void _onError(error) {
    print("Flutter Pedometer Error: $error");
  }

  //function to determine the distance run in kilometers using number of steps
  void getDistanceRun(double _numerox) {
    var distance = ((_numerox * 78) / 100000);
    distance = num.parse(distance.toStringAsFixed(2)); //dos decimales
    var distancekmx = distance * 1000000;//34;
    distancekmx = num.parse(distancekmx.toStringAsFixed(2));
    //print(distance.runtimeType);
    setState(() {
      _km = "$distance";
      //print(_km);
    });
    setState(() {
      _kmx = num.parse(distancekmx.toStringAsFixed(2));
    });
  }

  //function to determine the calories burned in kilometers using number of steps
  void getBurnedRun() {
    setState(() {
      var calories = _kmx; //dos decimales
      _calories = calories==null?"0":"$calories";
      //print(_calories);
    });
  }
Jeff
  • 161
  • 1
  • 2
  • 7

3 Answers3

1

As @Ride Sun suggests, you need to keep track of the steps you're saving and subtract that offset. If you want it to reset daily, you can find a complete walkthrough of how I did it here. This task was also tackled in this Github issue.

Roughly, the basic gist for tracking daily steps is this:

You'll need the following vars:

  • savedStepCount to save previous days' step count (e.g using shared_preferences). If you already save and display/use/show previous days' step count, you cannot use that var for this purpose, as this savedStepCount will reset intermittently

  • lastDaySaved, either an int or a DateTime to save the last time you "reset" your pedometer. this will become apparent shortly. persist this as well.

  • value is the count received from the pedometer stream

  • todayStepCount, self-descriptive.

       if (value < savedStepCount) {
         // Upon device reboot, pedometer resets. When this happens, the saved counter must be reset as well.
         savedStepCount = 0;
         // {persist this value using a package of your choice here}
       }

       var lastDaySaved;
       // {load the saved value using a package of your choice here}

       if (lastDaySaved < todayDayNo) { // whether you use int or DateTime, this should only return true once every 24 hours, otherwise
 your value will reset too frequently.

         lastDaySaved = todayDayNo
         // {save lastDaySaved here}
         savedStepCount = value;
         // {save savedStepCount here}
       }

       todayStepCount = value - savedStepCount;
       return todayStepCount; // this is your daily steps value.
MaskyS
  • 99
  • 5
0

By reading the source I would say it is continuous starting when the app is starting. You have to keep track of the count urself and remember the count when the user pressed reset like 11230 equals 0 and subtract that offset.

Ride Sun
  • 2,145
  • 3
  • 18
  • 41
-1

Basically, Flutter doesn't have a way to reset the pedometer data as the app is listening to the sensor data through a stream. So what you can do is simply declare a int variable "i" , and use this variable in the OnData function as "i+=1", and use this "i" as steps count and you can reset it by simply using the setState method as "i = 0", thus reseting the step count. What happens is the "i" gets incremented for every OnData function execution, which generally happens for every step you take and for reseting, just declare "i = 0".