Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Using async and await | Futures and async/await
Practice
Projects
Quizzes & Challenges
Quizer
Challenges
/
Dart Asynchronous Flow

bookUsing async and await

When working with asynchronous operations in Dart, you often need to handle values that are not immediately available, such as data from a network request or a file read. Dart provides the Future class for these operations. Traditionally, you might chain multiple asynchronous steps using the then method, which can quickly become difficult to read and maintain as the code grows in complexity.

The async and await keywords make asynchronous Dart code much easier to write and understand. You mark a function as async to indicate it returns a Future, and then use await before a Future-returning expression to pause execution until the operation completes. This allows you to write asynchronous code that looks and behaves much like synchronous code, making it easier to follow and reason about.

Using async and await helps you avoid deeply nested callbacks and improves error handling, since you can use try and catch blocks as you would in synchronous code. This leads to cleaner, more maintainable code, especially when you have to perform several asynchronous operations in sequence.

main.dart

main.dart

copy
123456789101112131415
Future<int> fetchNumber() async { // Simulate an asynchronous operation, such as fetching data from a server. await Future.delayed(Duration(seconds: 1)); return 42; } Future<void> printNumber() async { int result = await fetchNumber(); print('The number is $result'); } void main() async { await printNumber(); print('Done!'); }
question mark

What is the main purpose of using the async and await keywords in Dart?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 2

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

bookUsing async and await

Sveip for å vise menyen

When working with asynchronous operations in Dart, you often need to handle values that are not immediately available, such as data from a network request or a file read. Dart provides the Future class for these operations. Traditionally, you might chain multiple asynchronous steps using the then method, which can quickly become difficult to read and maintain as the code grows in complexity.

The async and await keywords make asynchronous Dart code much easier to write and understand. You mark a function as async to indicate it returns a Future, and then use await before a Future-returning expression to pause execution until the operation completes. This allows you to write asynchronous code that looks and behaves much like synchronous code, making it easier to follow and reason about.

Using async and await helps you avoid deeply nested callbacks and improves error handling, since you can use try and catch blocks as you would in synchronous code. This leads to cleaner, more maintainable code, especially when you have to perform several asynchronous operations in sequence.

main.dart

main.dart

copy
123456789101112131415
Future<int> fetchNumber() async { // Simulate an asynchronous operation, such as fetching data from a server. await Future.delayed(Duration(seconds: 1)); return 42; } Future<void> printNumber() async { int result = await fetchNumber(); print('The number is $result'); } void main() async { await printNumber(); print('Done!'); }
question mark

What is the main purpose of using the async and await keywords in Dart?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 2
some-alt