我在扑朔迷离地使用pedometer插件,我注意到的一件事是,过一会儿打开应用程序时,显示的步数直到我按顺序打开应用程序时才会更新,这不会更新触发StreamSubscription的onData方法。这不是setState
问题,因为它每秒更新一次。
以下是到目前为止我所做的与此相关的一部分:
class _MyScreenState extends State<MyScreen>
with AutomaticKeepAliveClientMixin<Page1>, WidgetsBindingObserver {
StreamSubscription _subscription;
final Store<AppState> store;
_MyScreenState(this.store, this.pedometer);
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
setUpPedometer();
}
void setUpPedometer() {
_subscription = pedometer.stepCountStream
.listen(_onData, onError: _onError, cancelOnError: true);
}
@override
Future<Null> didChangeAppLifecycleState(AppLifecycleState state) async {
if (state == AppLifecycleState.resumed) {
await resumeCallBack();
}
}
resumeCallBack() {
// here, I am looking for a way to trigger onData method
}
void _onData(int stepCountValue) async {
print("stepCountValue : ${stepCountValue}");
store.dispatch(UpdateStepsAction(
steps: stepCountValue));
}
}
答案 0 :(得分:0)
import 'dart:async';
import 'package:pedometer/pedometer.dart';
class PedometerController{
static Future<int> getStepsFromPedometer() async{
StreamController<int> streamHelper = new StreamController();
var pedometer = Pedometer();
pedometer.pedometerStream.listen((v){
streamHelper.sink.add(v);
streamHelper.close();
});
return await whenTrue(streamHelper.stream);
}
static Future<int> whenTrue(Stream<int> source) {
return source.firstWhere((int item) => item > -1);
}
}