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:-
1 2 3 4 5 6 7 8 9 10 11 |
import 'dart:io'; void main() { // prompt for user input print("Enter your favourite color :"); // this is a synchronous method that waits to read user input String color = stdin.readLineSync(); print("Your favourite color Is : ${color}"); print("End of main"); } |
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:-
1 2 3 |
func_name() async { // function body } |
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:-
1 2 3 |
void hello() async { print("Hello Dart"); } |
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:-
1 |
await e |
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:-
1 2 3 4 5 6 7 |
void hello() async { print("Hello Dart"); } void main() async { await hello(); print('All done'); } |
Output:-
1 2 |
Hello Dart All done |
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.