How to Use Async/Await In Dart?

12 minutes read

Async/await is a powerful feature in Dart that allows you to write asynchronous code in a more concise and readable manner. It is commonly used when performing time-consuming operations such as making API calls or accessing databases.


To use async/await in Dart, you need to understand a few key concepts:

  1. Async Functions: An async function is defined using the async keyword before the function body's return type. It can contain one or more await expressions, indicating points where the execution can pause and resume. An async function returns a Future that eventually resolves to a value or throws an error.
  2. Await Expressions: An await expression is used to pause the execution of an async function until the awaited operation completes. It can only be used inside an async function. When the awaited operation completes, the async function resumes from where it left off.


Here's an example of using async/await in Dart:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
Future<int> fetchData() async {
  // Simulating an asynchronous operation
  await Future.delayed(Duration(seconds: 2));
  return 42;
}

void main() async {
  print('Fetching data...');
  
  try {
    int result = await fetchData();
    print('Data fetched: $result');
  } catch (e) {
    print('Error: $e');
  }
  
  print('Program completed.');
}


In this example:

  • The fetchData() function returns a Future that resolves to an integer (in this case, 42) after a 2-second delay.
  • The main() function is marked as async to enable the use of await.
  • When await fetchData() is called, the execution of main() is paused until the Future returned by fetchData() is completed.
  • Once the Future is completed, the result is assigned to the result variable, and the execution continues.


Note that you can handle exceptions using a try-catch block when using async/await to handle errors gracefully.


Async/await plays a crucial role in simplifying asynchronous programming in Dart, providing a more intuitive and sequential code structure while preserving the benefits of non-blocking operations.

Best Dart Books to Read in 2024

1
Flutter and Dart Cookbook: Developing Full-Stack Applications for the Cloud

Rating is 5 out of 5

Flutter and Dart Cookbook: Developing Full-Stack Applications for the Cloud

2
Flutter Cookbook: Over 100 proven techniques and solutions for app development with Flutter 2.2 and Dart

Rating is 4.9 out of 5

Flutter Cookbook: Over 100 proven techniques and solutions for app development with Flutter 2.2 and Dart

3
Quick Start Guide to Dart Programming: Create High-Performance Applications for the Web and Mobile

Rating is 4.8 out of 5

Quick Start Guide to Dart Programming: Create High-Performance Applications for the Web and Mobile

4
Dart: Up and Running: A New, Tool-Friendly Language for Structured Web Apps

Rating is 4.7 out of 5

Dart: Up and Running: A New, Tool-Friendly Language for Structured Web Apps

5
The Dart Programming Language

Rating is 4.6 out of 5

The Dart Programming Language

6
Mastering Dart: A Comprehensive Guide to Learn Dart Programming

Rating is 4.5 out of 5

Mastering Dart: A Comprehensive Guide to Learn Dart Programming

7
Flutter Cookbook: 100+ step-by-step recipes for building cross-platform, professional-grade apps with Flutter 3.10.x and Dart 3.x, 2nd Edition

Rating is 4.4 out of 5

Flutter Cookbook: 100+ step-by-step recipes for building cross-platform, professional-grade apps with Flutter 3.10.x and Dart 3.x, 2nd Edition

8
Flutter for Beginners: An introductory guide to building cross-platform mobile applications with Flutter 2.5 and Dart, 2nd Edition

Rating is 4.3 out of 5

Flutter for Beginners: An introductory guide to building cross-platform mobile applications with Flutter 2.5 and Dart, 2nd Edition


What is the purpose of the await for loop in Dart's async/await?

The await for loop in Dart's async/await is used to iterate over the elements of a stream in an asynchronous manner.


With async/await, you can write asynchronous code that looks like synchronous code, making it easier to read and understand. The await for loop is used to await values emitted by a stream, one-by-one, and execute code for each value received.


Here's an example of how an await for loop can be used:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Stream<int> countStream() async* {
  for (int i = 1; i <= 5; i++) {
    await Future.delayed(Duration(seconds: 1));
    yield i;
  }
}

void main() async {
  await for (int count in countStream()) {
    print(count);
  }
}


In this example, countStream is a stream that emits integers from 1 to 5 with a delay of 1 second between each emission. The await for loop is used to iterate over the elements emitted by the stream and print each value received.


The await for loop can only be used with streams, and it will automatically handle the asynchrony of the stream by pausing execution until the next value is emitted.


How does async/await work in Dart?

In Dart, async/await is used to write asynchronous code in a more readable and synchronous style. It allows you to execute code asynchronously without blocking the main thread.


Here's how it works:

  1. Declare a function as asynchronous using the async keyword. For example: void main() async { // ... }
  2. Inside an asynchronous function, use the await keyword to pause the execution of code until a Future completes its execution and returns a value. For example: void main() async { String result = await fetchData(); print(result); } In this example, fetchData() is an asynchronous function that returns a Future. The await keyword waits for the Future to complete and assigns its value to the result variable.
  3. Asynchronous functions can also return a Future. For example: Future fetchData() async { // ... } In this case, the function signature specifies that it returns a Future of type String. The async keyword allows the function to use await inside it.
  4. You can also handle exceptions using try-catch blocks within an asynchronous function. When an exception is thrown, it can be caught and handled accordingly. For example: void main() async { try { String result = await fetchData(); print(result); } catch (e) { print('Error: $e'); } } Here, if an exception occurs during the execution of fetchData(), it will be caught in the catch block and an error message will be printed.


Async/await in Dart simplifies the process of managing asynchronous code by allowing you to write code that looks and behaves more synchronously, while still taking full advantage of the benefits of asynchronous programming.


How to use async/await in Dart?

The async/await syntax in Dart allows you to write asynchronous code in a more synchronous style, making it easier to read and understand. Here's how you can use async/await in Dart:

  1. Mark the function as async: void main() async { // ... }
  2. Use the await keyword to wait for the completion of an asynchronous operation. Example 1: Waiting for a future to complete: Future fetchData() async { await Future.delayed(Duration(seconds: 1)); return 42; } void main() async { int result = await fetchData(); print(result); // prints 42 } Example 2: Waiting for an asynchronous function to complete: Future fetchData() { return Future.value(42); } Future processResult() async { int result = await fetchData(); print(result); // prints 42 } void main() async { await processResult(); }
  3. Error handling: When an exception is thrown inside an async function, it's propagated as if you used a regular throw statement. To handle errors, you can use a try-catch block. Future fetchData() async { throw Exception('Error fetching data'); } Future processResult() async { try { await fetchData(); } catch (e) { print('Error: $e'); // prints "Error: Exception: Error fetching data" } } void main() async { await processResult(); }
  4. Benefits of using async/await: Provides a more synchronous style of writing asynchronous code. Easier to understand and reason about the control flow. Makes error handling straightforward using try-catch blocks.


Remember to always mark the function that uses await with the async modifier in order to use the await keyword.


How to call asynchronous functions using async/await in Dart?

To call asynchronous functions using async/await in Dart, you need to follow these steps:

  1. Make the function that calls the asynchronous function async by adding the async keyword before the function's return type.
  2. Use the await keyword before the asynchronous function call to pause execution and wait for the asynchronous operation to complete.


Here's an example of how to call an asynchronous function using async/await in Dart:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
Future<int> fetchNumber() async {
  // Simulating an asynchronous operation
  await Future.delayed(Duration(seconds: 2));
  
  return 42;
}

void main() async {
  // Call the asynchronous function
  int result = await fetchNumber();
  
  print(result);
}


In the example above, fetchNumber() is an asynchronous function that returns a Future<int>. Inside the main() function, await is used to pause execution until fetchNumber() completes. Once the asynchronous operation is finished, the value of result will be assigned 42 and printed.


How to handle multiple async errors in Dart using async/await?

To handle multiple async errors in Dart using async/await, you can use a try-catch block inside an async function. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Future<void> fetchData() async {
  try {
    String data = await fetchData1(); // async function that can throw an error
    processData1(data); // async function that can throw an error

    String moreData = await fetchData2(); // async function that can throw an error
    processData2(moreData); // async function that can throw an error

    // Other async operations...
  } catch (e) {
    // Handle individual errors or a specific type of error
    print('Error: $e');

    // Handle multiple errors at once if needed
    if (e is FetchData1Error) {
      // Handle error from fetchData1()
    } else if (e is ProcessData1Error) {
      // Handle error from processData1()
    } else if (e is FetchData2Error) {
      // Handle error from fetchData2()
    } else if (e is ProcessData2Error) {
      // Handle error from processData2()
    } else {
      // Handle other types of errors
    }
  }
}


In this example, each async function (fetchData1(), processData1(), fetchData2(), processData2()) represents an operation that can potentially throw an error. By using the await keyword, you can wait for each operation to complete before proceeding to the next line of code.


Inside the try block, you can call multiple async functions and perform other async operations. If any of them throw an error, the control flow will jump to the catch block, where you can handle the error appropriately.


You can handle individual errors or specific types of errors using conditional statements, such as if and else if. If needed, you can also handle multiple errors at once within the catch block.

Facebook Twitter LinkedIn Whatsapp Pocket

Related Posts:

To make API calls in Dart, you can follow these steps:Import the necessary packages: import &#39;package:http/http.dart&#39; as http; import &#39;dart:convert&#39;; Define a function to make the API call: Future makeApiCall() async { final String apiUrl = &#39...
Dart DevTools is a powerful tool for debugging Dart applications. It provides a comprehensive set of features to help you understand and analyze the behavior of your code during runtime. Here&#39;s an overview of how to use Dart DevTools for debugging:Installa...
To send emails via Gmail from Dart, you can use the mailer package. Here are the steps to achieve this:Set up your Dart project by creating a new Dart file and importing the necessary packages. import &#39;package:mailer/mailer.dart&#39;; import &#39;package:m...