Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 我如何实现一个定时器来在一定的延迟后执行代码?_Flutter_Dart_Timer - Fatal编程技术网

Flutter 我如何实现一个定时器来在一定的延迟后执行代码?

Flutter 我如何实现一个定时器来在一定的延迟后执行代码?,flutter,dart,timer,Flutter,Dart,Timer,我是一个新的开发人员使用颤振。在阅读了颤振文档和Stackoverflow之后,我无法解决我的问题。 我试图编写代码,显示一个屏幕,然后在执行print语句之前等待2秒钟。我错过了什么? 提前感谢你的帮助 import 'package:flutter/material.dart'; import 'dart:async'; class HomeScreen extends StatefulWidget { @override _HomeScreenState createState(

我是一个新的开发人员使用颤振。在阅读了颤振文档和Stackoverflow之后,我无法解决我的问题。 我试图编写代码,显示一个屏幕,然后在执行print语句之前等待2秒钟。我错过了什么? 提前感谢你的帮助

import 'package:flutter/material.dart';
import 'dart:async';

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  Timer timer;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0XFF5C894E),
      body: Column(
        children: [
          Container(
            alignment: Alignment.center,
            child: Image(
              image: AssetImage('images/Mezzo_Welcome_Screen.png'),
            ),
          ),
        ],
      ),
    );
  }

  void timeDelay() {
    timer = new Timer(const Duration(seconds: 2), () {
      print("This line will print after two seconds");
    });
    timeDelay();
    timer.cancel();
  }
}
导入“包装:颤振/材料.省道”;
导入“dart:async”;
类主屏幕扩展StatefulWidget{
@凌驾
_HomeScreenState createState()=>\u HomeScreenState();
}
类_homescrenstate扩展状态{
定时器;
@凌驾
小部件构建(构建上下文){
返回脚手架(
背景颜色:颜色(0XFF5C894E),
正文:专栏(
儿童:[
容器(
对齐:对齐.center,
孩子:图像(
image:AssetImage('images/Mezzo_Welcome_Screen.png'),
),
),
],
),
);
}
void timeDelay(){
计时器=新计时器(常数持续时间(秒数:2),(){
打印(“该行将在两秒钟后打印”);
});
延时();
timer.cancel();
}
}

使用定时器,以执行定期操作。请参阅代码片段

在应用程序的生命周期中初始化一次,将代码放在initState()中


虽然从您的问题/描述中还不清楚整个用例,但我认为没有必要使用计时器。改用延迟的未来,如下所示:

class\u homescrenstate扩展状态{
...
@凌驾
void initState(){
super.initState();
Future.delayed(持续时间(秒:2))。然后(()=>action();
}
无效行动(){
打印(“执行操作…”);
}
...
}

有两种方法可以做到这一点

  • 计时器
  • 未来
  • 计时器

    Timer(Duration(seconds: 2), () {
      print("Execute this code afer 2 seconds");
    });
    
    未来:

    Future.delayed(Duration(seconds: 2), () {
       print("Execute this code afer 2 seconds");
    });
    
    Timer(Duration(seconds: 2), () {
          print("Execute this code afer 2 seconds");
        }); 
     print("Other code");
    
    Other code
    Execute this code after 2 seconds
    
    Execute this code after 2 seconds
    Other code
    
    那么,上述两种方法的区别是什么

    带计时器

    Timer(Duration(seconds: 2), () {
      print("Execute this code afer 2 seconds");
    });
    
    计时器
    在给定的持续时间后运行其作业,但不会等待它完成其执行,它会执行以下语句

    示例:

    Future.delayed(Duration(seconds: 2), () {
       print("Execute this code afer 2 seconds");
    });
    
    Timer(Duration(seconds: 2), () {
          print("Execute this code afer 2 seconds");
        }); 
     print("Other code");
    
    Other code
    Execute this code after 2 seconds
    
    Execute this code after 2 seconds
    Other code
    
    输出:

    Future.delayed(Duration(seconds: 2), () {
       print("Execute this code afer 2 seconds");
    });
    
    Timer(Duration(seconds: 2), () {
          print("Execute this code afer 2 seconds");
        }); 
     print("Other code");
    
    Other code
    Execute this code after 2 seconds
    
    Execute this code after 2 seconds
    Other code
    
    所以,正如您所看到的,定时器将首先执行下面的代码,然后执行定时器。 此外,如果我们将计时器的对象装箱,计时器可以在执行之前的任何给定点停止

     Timer timer = Timer(Duration(seconds: 2), () {
              print("Execute this code afer 2 seconds");
            }); 
    timer.cancel();
    
    有未来

    future
    也会在给定的持续时间后运行它的作业,但是它的returnfuture对象意味着我们可以使用wait来首先执行它,然后执行下面的语句

     await Future.delayed(Duration(seconds: 2), () {
              print("Execute this code afer 2 seconds");
            });
            print("My Code");
        print("Other code");
    
    输出:

    Future.delayed(Duration(seconds: 2), () {
       print("Execute this code afer 2 seconds");
    });
    
    Timer(Duration(seconds: 2), () {
          print("Execute this code afer 2 seconds");
        }); 
     print("Other code");
    
    Other code
    Execute this code after 2 seconds
    
    Execute this code after 2 seconds
    Other code
    

    未来的主要缺点是我们不能在两者之间取消它。

    这非常有效!这不是正确的答案,因为它将每隔2秒定期执行一次操作,而作者只想执行一次操作。基本上,我只需要能够在执行代码的新部分之前有一个延迟。您的解决方案同样有效。非常感谢。问题是你怎么知道2秒钟就足够了。。。你将执行什么操作?应用程序将打开并加载显示主屏幕2秒钟,然后我将使用Navigator.push显示新路线。好的,那么我认为它应该对你有效,如果有效,你可以认为它是正确的solution@Manohara321我不确定你想要的答案是否是公认的。你声明了一个函数timeDelay(),但你从来没有真正调用过它。所以这毫无意义。@CEO tech4lifeapps我的问题已经解决了。见前面的评论。