问题描述
我为秒表创建了服务。
class StopWatchService {
String timetodisplay = '00:00:00';
Stopwatch swatch = Stopwatch();
final duration = const Duration(seconds: 1);
StreamController<String> stopWatchStreamController;
void startTimer() {
Timer(duration,keepRunning);
}
void keepRunning() {
if (swatch.isRunning) {
startTimer();
}
var hours = swatch.elapsed.inHours.toString().padLeft(2,"0");
var minutes = (swatch.elapsed.inMinutes % 60).toString().padLeft(2,"0");
var seconds = (swatch.elapsed.inSeconds % 60).toString().padLeft(2,"0");
timetodisplay = hours + ':' + minutes + ':' + seconds;
if (swatch.isRunning) {
stopWatchStreamController.add(timetodisplay);
}
else {
stopWatchStreamController.close();
}
// timeInSec = swatch.elapsed.inSeconds;
}
void start() {
swatch.start();
startTimer();
}
void stop() {
print('ride service stop ${swatch.isRunning}');
swatch.stop();
// stopWatchStreamController.close();
print('ride service stop 1 - ${swatch.isRunning}');
}
Stream<String> getStopWatchStream() {
stopWatchStreamController = StreamController<String>(
onListen: start,onCancel: stop,onResume: start,onPause: stop,);
return stopWatchStreamController.stream;
}
}
在我的mobx商店中,我已经添加了这些。
final RideService _rideService = RideService();
Stream<String> timerStream;
StreamSubscription<String> timerSubscription;
@observable
String movingTime;
@action
startTimer() {
timerStream = _rideService.getStopWatchStream();
timerSubscription = timerStream.listen((time) {
print('event - $time');
movingTime = time;
});
}
@action
stopTimer() {
print('stopTimer');
// _rideService.stop();
timerSubscription.cancel();
timerStream = null;
print('dsadsadadad ${timerSubscription}');
}
现在在小部件文件中,单击按钮时我正在调用startTimer,它工作正常。 但是当我在点击停止按钮时调用stopTimer时,计时器值仍在更新。
我不确定自己在做什么错。
谢谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)