【发布时间】:2019-10-07 18:54:09
【问题描述】:
我在flutter中使用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));
}
}
【问题讨论】:
-
不清楚,
_onData是否被调用?如果是,那是什么问题? -
好的,我会重新写问题。问题是我想在应用程序恢复时自己调用 _onData,没有真正的步骤
-
它是采用一个
int参数的常规方法......那么它有什么问题? -
问题是我想访问当前 stepCountValue 以便在应用程序恢复时更新当前步骤;否则,用户需要执行一些步骤才能调用 onData。他会看到他上次打开应用程序的步数,除非他这样做,否则不会显示在后台收集的步数。
-
和
pedometer.stepCountStream流总是 0、1、2、3 等?
标签: dart flutter subscription pedometer