小编moa*_*bly的帖子

有没有办法在关闭运行 flutter 应用程序的浏览器选项卡时显示警告对话框?

我想向用户表明,当他们尝试关闭我的 flutter web 应用程序中的选项卡时,正在下载一个文件。有没有办法连接到应用程序来检测这种行为并显示这样的警告消息?

警告弹出窗口

popup flutter flutter-web

7
推荐指数
1
解决办法
1515
查看次数

async/await/then 在 Dart 中如何真正工作?

这可能是一个反复出现的问题,但我发现了相互矛盾的答案,现在我很困惑哪个是正确的。我以为我理解了这个概念,然后我开始阅读所有这些答案并完全感到困惑,所以我正在寻找一个我可以轻松理解的明确简单的答案来解决我的问题。

根据这个答案这篇文章await应该中断代码执行并实际等待未来完成,然后继续按顺序执行其余代码。它还表明这可能会阻塞主线程,这仅在这种情况下才合乎逻辑。

另一方面,来自 flutter 团队的thisthis和this 视频表明,这不会阻止其余的代码执行,它只是注册回调以在 future 完成时执行的语法糖,这与做。await then

现在,我尝试编写一个小程序来了解其中哪一个是正确的,似乎第一种方法是正确的方法:

import 'dart:async';

// prints: 
// 1000+
// 1000+
void main() async {
  Stopwatch watch = Stopwatch();
  
  watch.start();
  
  await Future.delayed(Duration(seconds:1)).then((_){print(watch.elapsedMilliseconds);});
  
  print(watch.elapsedMilliseconds); 
  
}
Run Code Online (Sandbox Code Playgroud)

反对:

import 'dart:async';

// prints:
// 0
// 1000+
void main() async {
  Stopwatch watch = Stopwatch();
  
  watch.start();
  
  Future.delayed(Duration(seconds:1)).then((_){print(watch.elapsedMilliseconds);});
  
  print(watch.elapsedMilliseconds);
  
}
Run Code Online (Sandbox Code Playgroud)

所以我只想知道为什么 flutter …

multithreading async-await dart flutter

7
推荐指数
1
解决办法
3116
查看次数

标签 统计

flutter ×2

async-await ×1

dart ×1

flutter-web ×1

multithreading ×1

popup ×1