【问题标题】:What is the difference between Future.delayed vs Timer in flutterFlutter中的Future.delayed与Timer有什么区别
【发布时间】:2020-07-16 20:38:58
【问题描述】:

我想知道 Future.delayed 和 Timer 方法在延迟代码执行方面的区别。两者似乎都做同样的事情。

Future.delayed

Future.delayed(const Duration(milliseconds: 500), () { /*code*/ });

VS

定时器

Timer _timer = new Timer(const Duration(milliseconds: 500), () { /*code*/ });

【问题讨论】:

    标签: flutter dart time delay future


    【解决方案1】:

    对我来说有几个不同之处。

    • Future.of 返回一个 Future。
    • Timer 不返回任何内容。

    因此,如果您的延迟代码返回您继续工作所需的任何内容,Future 是您的最佳选择。


    其他区别是Timer 类提供了一种重复触发的方法。

    此引用来自Timer Class Reference 文档本身

    倒计时计时器,可配置为触发一次或重复

    Timer重复一起使用的示例可以是

    Timer.periodic(Duration(seconds: 5), (timer) {
        print(DateTime.now()); 
    });
    

    其他常见的示例是创建一个秒表,以测量代码中的时间,通常使用Timer

    GL!!

    【讨论】:

    • 另一个重要的区别是Timer 可以被取消。另外,为了测量时间,通常你应该使用Stopwatch 类。
    【解决方案2】:

    计时器:

    Timer() 创建一个Timer 对象,该对象会在延迟后运行您的计算。由于您获得了对该Timer 对象的引用,因此您可以选择在它被触发之前取消它,方法是调用cancel

    Timer t = Timer(Duration(seconds: 1), () => print("1 sec later"));
    t.cancel(); // nothing will be printed out
    

    未来:

    Future.delayed 创建一个Future,在延迟后运行其计算。 在内部,它仍然使用Timer 来执行此操作。它不会向您公开计时器,因此您无法控制或取消它。从好的方面来说,你可以做你正常的未来事情,比如await

    await Future.delayed(Duration(seconds: 1);
    print("1 sec later");
    

    【讨论】:

    • 我正在尝试使用 Future.delayed(Duration(seconds: 10 / 2), () {....10/2 在 Duration 秒内不起作用。请建议它的解决方案是什么?谢谢。
    • 我得到了解决方案。它接受int10/2double,我们可以使用10~/2 代替。谢谢。
    【解决方案3】:

    在以下情况下使用计时器:

    • 您希望能够取消它。使用Timer.cancel() 可以取消计时器,而Future 则必须使用CancelableCompleter 取消Future。

    • 如果您不想在回调方法中返回任何内容。

      例子:

      // Prints 'Hello' after 1s. 
      var timer = Timer(Duration(seconds: 1), () => print('Hello'));
      

      如果您决定取消它,请使用:

      timer.cancel();
      

    在以下情况下使用 Future:

    • 您的代码可能会抛出错误,而您想捕捉它们。如果您使用Timer 并且发生任何未捕获的异常,应用程序将退出。

    • 你想从你的回调方法中返回一些东西。

      例子:

      // Return 'Hello' after 1s and if there is any error, it will be caught. 
      Future
        .delayed(Duration(seconds: 1), () => 'Hello')
        .catchError((err) {}); 
      

    【讨论】:

      【解决方案4】:

      timer 在给定的持续时间之后运行它的作业,但颤动不等待它完成它的执行,它执行以下语句。

      示例:

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

      输出:

      Other code
      Execute this code after 2 seconds
      

      如您所见,计时器下面的代码将首先执行,然后再执行计时器。 此外,如果我们创建它的对象,Timer 可以在它执行之前的任何给定点停止。

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

      future 也在给定的持续时间之后运行它的工作,但它返回的未来对象意味着我们可以使用 await 来首先执行它,然后下面的语句将被执行。

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

      输出:

      Execute this code after 2 seconds
      Other code
      

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

      【讨论】:

      • 我正在尝试使用 Future.delayed(Duration(seconds: 10 / 2), () {....10/2 在 Duration 秒内不起作用。请建议它的解决方案是什么?谢谢。
      最近更新 更多