好坏:在Dart / Flutter中声明主要方法异步 [英] Good or Bad: Declaring main method async in Dart / Flutter

查看:389
本文介绍了好坏:在Dart / Flutter中声明主要方法异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在整个应用程序中声明一个全局变量- SharedPreferences首选项,并在 main 方法中对其进行初始化。 / p>

但是, SharedPreferences 初始化会返回 Future -因此,我尝试等待它在应用程序的 main 关闭中解决:

  SharedPreferences首选项; 

void main()async {
prefs = await SharedPreferences.getInstance();

runApp(MyApp());
}

它运行良好。我目前在生产中的2个应用程序中使用了此方法,但我突然想到使 main 方法异步可能是不正确的。



最后我有2个问题:




  • 的主要作用方法被调用,并且在Dart / Flutter中通常如何工作?

  • 将异步使应用程序的 main 方法带来意外的情况行为? (到目前为止)


解决方案


主要方法是否被调用以及它在Dart / Flutter中的一般工作方式?


Dart VM(或AOT模式下的运行时)看起来for并执行名为 main 的函数。在 main 返回之后,VM将退出之前等待等待的异步操作完成。 Dart官方网站上的异步编程文章中有一个示例演示了这一点: / p>



  1. main()完成执行后,异步功能可以恢复执行。首先,完成由 gatherNewsReports()返回的未来。然后 printDailyNewsDigest()继续执行,打印新闻。

  2. printDailyNewsDigest()函数主体完成执行,它最初返回的未来完成,并且应用程序退出。


(请注意, exit 函数将立即终止,而无需等待。)







使应用程序的主要方法异步带来意外行为吗? (到目前为止)


否。首先,请记住, async 关键字是 not ,它使函数异步。 async 关键字简单地允许使用 await 关键字(它本身是用于注册<$ c $的语法糖) c> Future.then 回调),并且(大多数情况)要求声明该函数以返回 Future 。 (我之所以说大部分,是因为虽然返回 void 而不是 Future< void> 如果 <$ c,> dartanalyzer 也会对此抱怨。 $ c> avoid_void_async lint 已启用。)



一旦您调用 any 异步函数。调用异步函数时,您可以:




  • 等待其完成(通过 await Future.then )。然后,调用方也是异步的。

  • 不需要等待异步操作(即发即忘)。但这仍然意味着 main 可以在异步操作仍未完成的情况下返回。



无论哪种方式,您的应用程序都必须等待终止(假设它没有遇到因未捕获的异常或 exit 而导致的异常终止)。



由于您的 main 函数使用 await ,因此您甚至没有选择标记异步


I declare one global variable in the entire app - SharedPreferences prefs, and initialize it in main method.

However, SharedPreferences initialization returns a Future - hence I tried awaiting for it to be resolved in main closure of the app:

SharedPreferences prefs;

void main() async {
  prefs = await SharedPreferences.getInstance();

  runApp(MyApp());
}

And it worked well. I currently use this method in my 2 apps in production, and it just occurred to me that making main method asynchronous might not be right.

In the end I have 2 questions:

  • How does main method gets invoked and how it works in general in Dart / Flutter?
  • Will making main method of the app asynchronous bring unexpected behaviour? (it hasn't so far)

解决方案

How does main method gets invoked and how it works in general in Dart / Flutter?

The Dart VM (or the runtime in AOT mode) looks for and executes a function named main. After main returns, the VM will wait for pending asynchronous operations to complete before exiting. The Asynchronous programming article on the official Dart website has an example that demonstrates this:

  1. When main() has finished executing, the asynchronous functions can resume execution. First, the future returned by gatherNewsReports() completes. Then printDailyNewsDigest() continues executing, printing the news.
  2. When the printDailyNewsDigest() function body finishes executing, the future that it originally returned completes, and the app exits.

(Note that the exit function will cause immediate termination without any waiting.)


Will making main method of the app asynchronous bring unexpected behaviour? (it hasn't so far)

No. First, you should keep in mind that the async keyword is not what makes a function asynchronous. The async keyword simply enables the use of the await keyword (which itself is syntactic sugar for registering a Future.then callback) and (mostly) requires that the function be declared to return a Future. (I say "mostly" because returning void instead of Future<void> is allowed, although dartanalyzer will complain about that too if the avoid_void_async lint is enabled.)

Your application will inherently be asynchronous once you invoke any asynchronous function. When you invoke an asynchronous function, you either:

  • Wait for it to complete (via await or Future.then). The caller is then asynchronous too.
  • The asynchronous operation is unawaited ("fire-and-forget"). But this still means that main can return with asynchronous operations still pending.

Either way, your application must wait before terminating (assuming that it didn't encounter abnormal termination from an uncaught exception or exit).

Since your main function uses await, you don't even have a choice about marking it async.

这篇关于好坏:在Dart / Flutter中声明主要方法异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆