Dart Async

In this tutorial you will learn about the Dart Async and its application with practical example.

Dart Async

Dart is a single threaded programming language that helps the developer ensure that critical functions to be executed till completion, without preemption. In Dart, one operation is executed at a time, thereafter after the other operation; that mean as long as one operation is executing, it cannot be interrupted by any other part of the program. Let’s take an example to understand this concept.

Example:-

Here, we are using the “dart:io” library to accepts the user input. In the above program, the readLineSync() method is a synchronous method. Since it is a synchronous method, this will block the execution of all instructions after the readLineSync() function till it completes its task. In the above program the stdin.readLineSync function waits for the user input; it block the program execution and hold the execution until it receives the user’s input.

Dart Asynchronous programming

To help keep the application responsive, Dart uses an asynchronous programming model, which let your program run without getting blocked. In asynchronous programming, when program calls a method to perform an operation asynchronously, the program continue executing while the asynchronous method performs its task. An asynchronous operation is executed in a separate thread from the main application thread. In Dart, the dart:async package facilitates implementing asynchronous programming.

Dart Future

Dart uses Future objects (futures) to facilitate the asynchronous programming. A Future objects (Future<T>) represents an asynchronous operations that will produces a result of type T. To work with futures, you can use either async and await or the Future API.

Dart async and await

In Dart, the async and await keywords facilitate the asynchronous programming without using the Future API. When you want a function to run asynchronously, you need to have the async keyword added before its body. This async keyword comes right after the function signature.

Syntax:-

When an async function is invoked, a Future object is immediately returned and the function is executed later. As the body of the async function is executed, the Future object returned by the function call will be completed along with its result.

Example:-

Dart await Keyword

The await keyword executes a function asynchronously, and then suspends the currently running function until the result is ready, after that continue on to the next line of code. The await keyword only works in async functions.

Syntax:-

Here, e is an asynchronous expression and is expected to evaluate to a Future. The await expressions evaluate e, and then suspends the currently running function until the result is ready.

Example:-

Output:-

Here, we have used the async keyword with main method because we are going to call the hello() function asynchronously. Then, we have used await modifier directly in front of the hello() function that we want to run asynchronously.

Note :- When you are using await, make sure caller function and the function called must use the async modifier.

In this tutorial we have learn about the Dart Async and its application with practical example. I hope you will like this tutorial.