async In Dart

async In Dart :

  • Dart is single threaded programming language that helps the developer ensure that critical functions to be executed till completion or not.
  • One operation is executed at a time, thereafter after the other operation that means as long as one operation is executing, it cannot be interrupted by any other part of the program.
import 'dart:io';

void main() {
  String color = stdin.readLineSync();
  print(color);
}
  • Here, we are using the ‘dart:io’ library to accepts the user input. In the above program, the readLineSync() is synchronous method.
  • Sines it is synchronous, this will block the execution of all instructions after the readLineSync() 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.
async In Dart

Dart Async programming

  • Dart uses an async programming model, which is program run without getting blocked.
  • In async programming, when program calls a method to perform an operation async the program continue executing while the asyc perform its task.
  • An async operation is executed in a separate thread from the main application thread.
  • That is implement dart:async package.

Dart Future :

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

Dart async :

function_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.

Dart await keyword :

  • await executes a function async and then suspends the currently running function until the result is ready, after that continue on to the next line of the code. That is used to async functions.

Syntax :

await e

  • Here e is an async 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.
void hello() async{
  print('Hello Dart');
}

void main() async{
await hello();
  print('All done');
}

The flutter tutorial  is a website that bring you the latest and amazing resources of code. All the languages codes are included in this website. The languages like flutter, android, java,kotlin etc.with the help of this languages any user can develop the beautiful application

For more information about Flutter. visit www.fluttertutorial.in