Table of contents
1.
Introduction
2.
Async Functions
3.
Await Keyword
4.
Future Keyword
5.
Frequently Asked Questions
5.1.
Why do we use the async modifier in the main function?
5.2.
What are the most common methods to handle Futures?
5.3.
How does the await keyword work?
6.
Conclusion 
Last Updated: Mar 27, 2024

Dart Async

Author Pradeep Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we will look into the Dart Async. Dart Async is associated with asynchronous programming. What is asynchronous programming, you might wonder? First, let's address that question. There are two kinds of programming: synchronous and asynchronous programming. 

                                                     

In synchronous programming, we wait for an event to finish before moving forward (i.e., until that even ends, no further execution of any other event is done). This approach creates a problem while entertaining requests from multiple concurrent users. Even though the requests received from concurrent users are independent, we are still waiting for one request to finish before executing another, which creates an unnecessary delay.

To solve this problem, asynchronous programming is used. Asynchronous programming is non-blocking (i.e., it doesn't wait for an event to finish before executing another event). It carries out the even in a thread different from the main program thread. Since all the threads are independent of each other and are running concurrently, it solves the problem of delay.

If you are new to Dart and are yet to set up your environment for the language, you can check out this article.

Async Functions

Asynchronous programming is built on the foundation of functions. The body of these async functions contains async modifiers. The async modifier must be added with any functions you want to perform asynchronously. This modifier appears immediately after the function's signature. The syntax of an async function is as follows:

Syntax:

// Asynchronous Function
asyncFunction() async {
  // body of the function
}

Await Keyword

The await keyword is used to execute async functions. It suspends the presently executing function and waits until the result of that async function is ready. It moves on to the next line of code once the result of the async function is returned. The await keyword is used only with async functions. For example,

// Asynchronous Function
asyncFunction() async {
  print("Inside async function");
}

// Main function to run the program
void main() async {
  // print statement before executing the async function
  print("Before Async Function");
  await asyncFunction(); // Executing the async function
  // print statement after executing the async function
  print("After Async Function");
}


Output:

Before Async Function
Inside async function
After Async Function

Future Keyword

Dart is a programming language with only one thread. The Future<T> object describes the outcome of an asynchronous operation that yields a result of type T. In case there is no useful value to be returned, the type of future is Future<void>. A Future asynchronously represents a single value, either data or an error. For example,

// Asynchronous Function
Future asyncFunction() async {
  final duration = Duration(seconds: 3);
  String message = "Inside async function";
  // It waits for 3 seconds before returning the answer
  return Future.delayed(duration).then((value) => message);
}

// Main function to run the program
void main() async {
  // print statement before executing the async function
  print("Before Async Function");
  await asyncFunction().then((result) {
    // It waits for the result and prints when the result is returned
    print(result);
  }); // Executing the async function
  // print statement after executing the async function
  print("After Async Function");
}


Output:

Before Async Function
Inside async function
After Async Function

Frequently Asked Questions

Why do we use the async modifier in the main function?

As the main function is executing an asynchronous function, it is mandatory to add the async modifier to the main function.

What are the most common methods to handle Futures?

Using the async and await keywords, as well as the Future API, are two of the most prevalent approaches for dealing with Futures.

How does the await keyword work?

Await effectively stops the control flow until the procedure is finished. To utilize await within a function, the function must be marked with the async keyword.

Conclusion 

In this article, we have extensively discussed the Async functions in Dart. We hope that this blog has helped you enhance your knowledge regarding the Dart Async, and to learn more, check out our other article on Dart Operators.
And also, check out the awesome content on the Coding Ninjas Website,
Android DevelopmentCoding Ninjas Studio ProblemsCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview ExperiencesCoding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test SeriesDo upvote our blog to help other ninjas grow.
Happy Coding!

Live masterclass