Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Using async and await | Futures and async/await
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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 2

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

bookUsing async and await

Scorri per mostrare il menu

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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 2
some-alt