gpt4 book ai didi

flutter - flutter :时间间隔未按预期工作

转载 作者:行者123 更新时间:2023-12-03 04:34:43 26 4
gpt4 key购买 nike

我想每2秒做点什么,所以我使用了以下代码:

Timer.periodic(Duration(seconds: 5), (timer) {
print(DateTime.now());
});
但我得到的结果是出乎意料的:
I/flutter ( 3289): 2020-10-12 21:22:43.670760
I/flutter ( 3289): 2020-10-12 21:22:44.775357
I/flutter ( 3289): 2020-10-12 21:22:48.648645
I/flutter ( 3289): 2020-10-12 21:22:49.777089
I/flutter ( 3289): 2020-10-12 21:22:53.648152
I/flutter ( 3289): 2020-10-12 21:22:54.776803
I/flutter ( 3289): 2020-10-12 21:22:58.648512
I/flutter ( 3289): 2020-10-12 21:22:59.775297
I/flutter ( 3289): 2020-10-12 21:23:03.647867
I/flutter ( 3289): 2020-10-12 21:23:04.775481
我的完整代码
class test extends StatelessWidget {
var i ;
time(){
Timer.periodic(Duration(seconds: 5), (timer) {

print(DateTime.now());
});
}
@override
Widget build(BuildContext context) {
time();
return Container();
}
}
如您所见,我想在5秒的间隔内打印时间,但它会做其他事情。我不知道为什么,有人可以告诉我我应该怎么做才能达到我的期望。
提前致谢

最佳答案

运行flutter clean并卸载以前的应用,然后再次运行

@override
void initState() {
super.initState();
Timer.periodic(Duration(seconds: 5), (timer) {
print(DateTime.now());
});
}
工作实例
Screenshot

关于flutter - flutter :时间间隔未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64318713/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com