Dive deep into the Dart `async` function and learn how to accurately utilize `then` with futures without blocking your application's execution!
---
This video is based on the question https://stackoverflow.com/q/63310372/ asked by the user 'carlao2005' ( https://stackoverflow.com/u/5046080/ ) and on the answer https://stackoverflow.com/a/63310501/ provided by the user 'Rémi Rousselet' ( https://stackoverflow.com/u/8394265/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Trying to understand async and then
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Understanding the async and then in Dart: A Simple Guide
When coding in Dart, particularly when using Flutter, it’s crucial to understand how async functions work alongside futures and the then method. Many developers encounter issues that stem from simple misunderstandings, especially when it comes to blocking operations. In this post, we’ll explore a common problem encountered while using async, followed by a clear solution. Let’s dive in!
The Problem
Consider the following Dart code:
[[See Video to Reveal this Text or Code Snippet]]
In the code snippet above, we have an asynchronous function f() that simulates a delay of 3 seconds before returning the integer 7. Meanwhile, in the main() function, we initialize a variable a with the value 3. We call function f() and store its returned future in b. The expectation here is that we should see 3 initially, followed by b value received: 7, and finally 7, followed by end at the end. However, the output is not as expected:
[[See Video to Reveal this Text or Code Snippet]]
What Went Wrong?
The confusion arises from the use of the sleep function. Unlike await, which only pauses the execution of a single async function, sleep blocks the entire app, halting all asynchronous operations. This means that while the app is asleep, no values can be processed or printed from your future. Thus, the outcome is that we only see the initial value of a and not the updated one after the future completes.
The Solution
To solve this issue and achieve the expected output, we need to avoid using sleep. Instead, we can utilize await Future.delayed. Here’s how you can rewrite the code:
[[See Video to Reveal this Text or Code Snippet]]
Expected Output
Running the updated code will produce the expected results:
[[See Video to Reveal this Text or Code Snippet]]
Conclusion
By avoiding the blocking sleep function and properly using await to manage asynchronous code execution, we can ensure our application behaves as expected. Understanding the differences between blocking and non-blocking calls is essential, especially when working with futures in Dart.
When faced with async challenges, remember to keep your code non-blocking, and always favor await over sleep to maintain the flow of your application!
Happy coding!
Информация по комментариям в разработке