首页 > 解决方案 > 使用 Flutter DateTime 和 Timer 时遇到不一致的行为

问题描述

在使用 Flutter 的 DateTime 类时,我注意到一些不一致的地方。Timer 和 DateTime 似乎不一致。当我打印当前秒数时,我看到输出不是我期望看到的。我需要这方面的帮助。这是我的代码:

    DateTime now = new DateTime.now();

    print(now.second.toString());
    print('SENDING POS');
    
    Timer(Duration(seconds: 3), () {
    // 3s over, send the other message

    DateTime now = new DateTime.now();

    print(now.second.toString());
    print('POS DONE');
    print('SENDING REFL');
});

Timer(Duration(seconds: 3), () {
    // 3s over, send the other message

    DateTime now = new DateTime.now();
    print(now.second.toString());
    // 5s over, send the other message
    print('REFL DONE');
    print('SENDING PROJ LINES');
});

Timer(Duration(seconds: 15), () {
    // 15s over, send the other message

    DateTime now = new DateTime.now();

    print(now.second.toString());
    print('PRJL DONE');
    print('SENDING HEIG');
});

Timer(Duration(seconds: 15), () {
    // 15s over, send the other message

    DateTime now = new DateTime.now();

    print(now.second.toString());
    pdps.dismissDataBundleDialog(pdps.context);

    print('HEIG DONE');
});

我的预期输出应该是:

    2
SENDING POS
5
POS DONE
SENDING REFL
8
REFL DONE
SENDING PROJ LINES
23
PRJL DONE
SENDING HEIG
38
DISMISS
HEIG DONE

但我看到的是:

2
SENDING POS
5
POS DONE
SENDING REFL
5
REFL DONE
SENDING PROJ LINES
17
PRJL DONE
SENDING HEIG
17
DISMISS
HEIG DONE

我该如何解决这种行为?

提前致谢

标签: flutterdatetimetimeroutput

解决方案


使用 then() 链解决它:

DateTime now = new DateTime.now();

                  print(now.second.toString());
                  print('SENDING POS');
                  Future.delayed(Duration(seconds: 3), () {
                    // 3s over, send the other message
                    DateTime now = new DateTime.now();
                    print(now.second.toString());
                    print('POS DONE');
                    print('SENDING REFL');
                  }).then((value) => {
                        Future.delayed(Duration(seconds: 3), () {
                          // 3s over, send the other message
                          DateTime now = new DateTime.now();
                          print(now.second.toString());
                          // 5s over, send the other message
                          print('REFL DONE');
                          print('SENDING PROJ LINES');
                        }).then((value) => {
                              Future.delayed(Duration(seconds: 15), () {
                                // 15s over, send the other message
                                DateTime now = new DateTime.now();
                                print(now.second.toString());
                                print('PRJL DONE');
                                print('SENDING HEIG');
                              }).then((value) => {
                                    Future.delayed(Duration(seconds: 15), () {
                                      // 15s over, send the other message
                                      DateTime now = new DateTime.now();
                                      print(now.second.toString());
                                      pdps.dismissDataBundleDialog(
                                          pdps.context);
                                      print('HEIG DONE');
                                    })
                                  })
                            })
                      });

推荐阅读