gpt4 book ai didi

flutter - 如何重置步数计数器 flutter

转载 作者:行者123 更新时间:2023-12-03 04:00:17 25 4
gpt4 key购买 nike

我想使用 flutter 来计算步骤
并找到了这个插件
https://pub.dev/packages/flutter_pedometer

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

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

if you have another way or plugin please share it



更新仅当我重新启动设备时才会重置。
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'),
),
),
);
}
}

最佳答案

您可以使用stopListening进行_subscription.cancel();

关于flutter - 如何重置步数计数器 flutter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57731847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com