I am using pedometer plugin in flutter, and one thing I noticed is that when I open the app after a while, the number of steps displayed does not update until I make steps while the app is opened in order to trigger onData method of the StreamSubscription. It is not setState
problem because it updates every second.
Below is part of what I did related to this until now:
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));
}
}
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.