One connection to asynchronous programming is Dart Async. It uses a thread to carry out the asynchronous action. It guarantees that the essential tasks are carried out all the way through. The primary application thread is not involved in the asynchronous operation’s execution. One action in Dart cannot impede the execution of another; that is, only one operation can run concurrently, and no other program element can stop it. Let’s examine the subsequent illustration:

Example

				
					import 'dart:io';   
void main() {   
   print("Enter your favorite car :");              
     
   // prompt for user input   
   String car = stdin.readLineSync();    
     
   // this is a synchronous method that reads user input   
   print("The car is  ${car}");   
   print("End of main");   
}  
				
			

Output

				
					Enter your favorite car :
Renault Duster
The car is Renault Duster
End of main
				
			

Explanation

The synchronous readLineSync() technique was utilized in the code above. This indicates that until the readLineSync() method finishes executing, all instructions that come after it will not be able to be executed.

Until it receives the user’s input, the stdin.readLineSync() function does nothing. In order to continue, it waits for user input.

Difference between Asynchronous and Synchronous

Let’s examine the distinction between asynchronous and synchronous processes.

When a program is described as synchronous in computer science, it indicates that it waits for an event to occur before continuing to run. This method has a drawback in that it prevents subsequent blocks from executing through unrelated blocks if a portion of the code takes a long time to run.

This is the synchronous approach’s primary issue. The synchronous technique prevents a program segment from running before the current segment, even though it might be necessary.

For web servers, where requests must be independent of one another, this is not appropriate. This indicates that the web server answers requests from other users before waiting to complete the execution of the current request.

Prior to processing the previous requests, the web server ought to approve the request made by the other user.

We refer to this method as asynchronous programming. Programming that does not wait or block is the main focus of asynchronous programming. In a Dart script, the asynchronous programming block can be implemented with the help of the dart: async module.

Example

We develop a program to read the names.txt file, which we generate with a few names in it, without interfering with the other parts of the code.

				
					1, Peter  
2, John  
3, Tom  
4, Johnson  
				
			

Take a look at the code below

				
					import "dart:async";   
import "dart:io";    
  
void main(){   
   File file1 = new File("C:\Users\DEVANSH SHARMA\Desktop\contact.txt");   
   Future<String> fs = file1.readAsString();    
    
   // returns a future object, it is an async method   
   fs.then((data)=>print(data));    
     
   // once file is read, call back method is invoked    
   print("End of main");    
   
} 
				
			

Output

				
					End of main
1, Peter
2, John
3, Tom
4,  Johnson
				
			

Dart Future

Receiving a result at some point in the future is the definition of the Dart Future. The Future object makes asynchronous programming easier to do. Future objects are a useful tool for indicating values that an expression returns and whose execution will end at a later time (In Future). Either the Future API or async and await can be used to work with the future.

Dart async and await

It is possible to implement asynchronous programming without utilizing the Future API by using the async and await keywords. In order to execute a function asynchronously, the async keyword must come after the function name. Below is the syntax:

Syntax

				
					func_name() async {  
   //function body  
} 
				
			

The Future object immediately returns once an async function is called, indicating that the async function will run later. The Future object was returned by the function call when the async function’s body was completed. The outcome of the function call will complete the call.

Dart await Keyword

An additional method for asynchronous function execution is the await keyword. Until the outcome is ready, it pauses the function that is now running. It moves on to the following line of code after returning the outcome. Async functions are the only ones that support the await keyword. Below is the syntax.

Syntax

				
					await e;  
				
			

It is anticipated that the asynchronous expression e will evaluate to a Future in this case. After evaluating e, the await expression pauses the active function until the outcome is ready.

Example

				
					void hii() async {  
         print("Hii Javatpoint");  
  
}  
  
void main() async {  
         await hii();            // Using await keyword  
         print("Task Complete");  
  
}  
				
			

Output

				
					Hii JavaTpoint
Task Complete
				
			

Explanation

Since the hii() method is called asynchronously, we have used the async keyword to declare the main() function as asynchronous. Next, we called the asynchronous hii() function using the await modifier.

Note: In order to use await, both the called and caller functions must use the async keyword.

Share this Doc

Dart Async

Or copy link

Explore Topic