如何重设步数计数器颤动

时间:2019-08-30 18:46:05

标签: flutter dart

我想用颤振计算步数 并找到了这个插件 https://pub.dev/packages/flutter_pedometer

但是即使我关闭应用程序也无法将计数器重置为0,它仍然是旧计数器

我试图再次调用initPlatformState()方法,但是没有用

  

如果您有其他方法或插件,请分享

更新,仅当我重新启动设备时才会重置。

import 'package:flutter/material.dart';//
import 'dart:async';//

import 'package:flutter_pedometer/flutter_pedometer.dart';//package name

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _stepCountValue = 'Unknown';
  StreamSubscription<int> _subscription;

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

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    FlutterPedometer pedometer = new FlutterPedometer();
    _subscription = pedometer.stepCountStream.listen(_onData,
        onError: _onError, onDone: _onDone, cancelOnError: true);

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;
  }

  void _onData(int stepCountValue) async {
    print(stepCountValue);

    setState(() {
      _stepCountValue = "$stepCountValue";
    });
  }

  void _onDone() {}

  void _onError(error) {
    print("Flutter Pedometer Error: $error");
  }
//
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: const Text('Plugin example app'),
        ),
        body: new Center(
          child: new Text('Steps taken: $_stepCountValue\n'),
        ),
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:0)

您可以stopListening_subscription.cancel();