【问题标题】:How to reset Step Count value to 0 on Flutter Pedometer?如何在 Flutter 计步器上将 Step Count 值重置为 0?
【发布时间】:2019-09-02 01:09:48
【问题描述】:

我在我的 Flutter 应用程序上使用计步器插件 (https://pub.dev/packages/pedometer#-installing-tab-)。有人可以回答我如何重置步数值吗?已尝试使用计时器将值重置为 0,但该值仍来自最后一步计数。

这是我用来测试计步器的代码:

请帮助我,我是新手。谢谢

@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);
    });
  }

【问题讨论】:

    标签: android flutter pedometer


    【解决方案1】:

    正如@Ride Sun 建议的那样,您需要跟踪您正在保存的步骤并减去该偏移量。如果您希望它每天重置,您可以找到我如何做到这一点的完整演练here。这个任务也在这个 Github issue 中得到解决。

    大致而言,跟踪每日步数的基本要点如下:

    你需要以下变量:

    • savedStepCount 保存前几天' 步数(例如使用 shared_preferences)。如果您已经保存并 显示/使用/显示前几天的步数,您不能使用该变量 为此,因为此 savedStepCount 将间歇性重置

    • lastDaySaved,int 或 DateTime,用于保存您上次的时间 “重置”您的计步器。这很快就会变得明显。坚持 这也是。

    • value 是从计步器流中接收到的计数

    • tod​​ayStepCount,自我描述。

           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.
    

    【讨论】:

    • 如何按天、周、月存储这个值??
    【解决方案2】:

    通过阅读源代码,我会说它在应用程序启动时连续启动。您必须自己跟踪计数并记住用户按下重置时的计数,例如 11230 等于 0 并减去该偏移量。

    【讨论】:

      【解决方案3】:

      基本上,Flutter 无法重置计步器数据,因为应用程序正在通过流监听传感器数据。所以你可以做的就是简单地声明一个int变量“i”,并在OnData函数中使用这个变量作为“i+=1”,并使用这个“i”作为步数,你可以通过简单地使用setState方法来重置它作为“i = 0”,从而重置步数。发生的情况是每次执行 OnData 函数时“i”都会递增,这通常发生在您执行的每一步和重置时,只需声明“i = 0”。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-10-28
        • 1970-01-01
        • 2021-03-11
        • 1970-01-01
        • 1970-01-01
        • 2022-08-19
        • 1970-01-01
        相关资源
        最近更新 更多