In Dart, asynchronous operations are handled using asynchronous programming techniques, which allow you to perform operations without blocking the execution of your code. Asynchronous programming is particularly useful when dealing with time-consuming tasks such as network requests, file operations, or interactions with databases. Dart provides several mechanisms to handle asynchronous operations:
1. Future and async/await : Dart utilizes the `
Future
` class, which represents a value that may not be available immediately. The `
async
` and `
await
` keywords are used together to work with asynchronous operations in a synchronous manner, making the code more readable and easier to follow.
* `async`: The `
async
` keyword is used to mark a function as asynchronous. It allows you to use the `
await
` keyword within the function body to wait for the completion of asynchronous operations.
* `await`: The `
await
` keyword is used to pause the execution of an `
async
` function until a `
Future
` completes. It allows you to extract the result of the `Future` once it's available.
Here's an example that demonstrates the usage of `
async
` and `
await
`:
Future<void> fetchData() async {
// Simulating an asynchronous operation
await Future.delayed(Duration(seconds: 2));
print('Data fetched!');
}
void main() {
print('Start');
fetchData();
print('End');
}​
Output :
Start
End
Data fetched!​
In the example above, the `
fetchData
` function is marked as `
async
`, and the `
await
` keyword is used to pause the execution until the `
Future.delayed
` operation completes. The delay simulates an asynchronous operation, and once the delay is over, the code resumes execution, and "
Data fetched!
" is printed.
2. Callbacks and Futures : Dart also provides the option to handle asynchronous operations using callbacks and `
Future
` objects directly. This approach involves registering callbacks to be executed when a `
Future
` completes.
void fetchData() {
Future.delayed(Duration(seconds: 2)).then((_) {
print('Data fetched!');
});
}
void main() {
print('Start');
fetchData();
print('End');
}​
Output :
Start
End
Data fetched!​
In this example, the `
then
` method is used to register a callback to be executed when the `
Future.delayed
` operation completes. Once the delay is over, the callback is invoked, and "
Data fetched!
" is printed.
3. Streams : Dart provides a `
Stream
` class to handle continuous asynchronous data streams, such as real-time data feeds or event streams. Streams are a sequence of asynchronous events that can be listened to and processed.
Stream<int> countDown(int from) async* {
for (int i = from; i >= 0; i--) {
await Future.delayed(Duration(seconds: 1));
yield i;
}
}
void main() {
print('Start');
countDown(5).listen((value) {
print(value);
});
print('End');
}​
Output :
Start
End
5
4
3
2
1
0​
In this example, the `
countDown
` function returns a `
Stream
` that emits integers from a given number down to zero. The `
listen
` method is used to subscribe to the stream and receive the emitted values. As the countdown progresses, the callback inside `
listen
` is invoked for each emitted