正如你在图像部分看到的,我打印了我传递给函数1的任何值]1这是我用来计时器的代码片段。我将这个函数作为布尔值传递,当我需要启动计时器时,传递true
值,这个条件工作得很好。但当此函数接收到false
值时,计时器不会停止。我也尝试了一些其他的逻辑,但仍然不起作用。
请告诉我我哪里错了?
Timer _timer;
startTimer(value) {
print("Recording values...........$value");
const oneSec = const Duration(seconds: 1);
_timer = new Timer.periodic(
oneSec,
(Timer timer) {
if (mounted && value == true) {
setState(() {
seconds = seconds + 1;
});
} else if (mounted && value == false){
setState(() {
_timer.cancel();
});
}
},
);
}
发布于 2021-04-06 11:07:32
startTimer(value) {
print("Recording values...........$value");
const oneSec = const Duration(seconds: 1);
if (_timer != null && !value) _timer.cancel();
_timer = Timer.periodic(
oneSec,
(Timer timer) {
if (mounted && value) {
setState(() {
seconds++;
});
}
},
);
}
https://stackoverflow.com/questions/66966337
复制相似问题