Dart Core Libraries 'dart:async'
The dart:async
library provides essential tools for managing asynchronous programming, enabling developers to write efficient and responsive Dart applications.
Asynchronous programming in Dart allows tasks to run concurrently without blocking the main execution thread. This approach is essential for building responsive applications, especially in scenarios where operations like fetching data from a remote server can take an unpredictable amount of time.
Key Components of dart:async
1. Futures and Completers
Future<T>
: Represents a value or error that will be available in the future. It is commonly used to represent the result of an asynchronous operation.Completer<T>
: Allows manual control over the completion of aFuture
, enabling the creation of custom asynchronous workflows.
Example - Using Future
for Delayed Execution:
import 'dart:async';
void main() {
Future.delayed(Duration(seconds: 2), () {
print('Delayed message after 2 seconds');
});
print('Main execution continues...');
}
2. Streams and StreamControllers
Stream<T>
: Represents a sequence of asynchronous events over time. It can emit multiple values asynchronously.StreamController<T>
: Manages aStream
and allows adding events to the stream programmatically.
Example - Creating and Listening to a Stream:
import 'dart:async';
void main() {
StreamController<int> controller = StreamController<int>();
// Add events to the stream
controller.sink.add(1);
controller.sink.add(2);
controller.sink.add(3);
// Listen to the stream
controller.stream.listen((data) {
print('Received: $data');
});
// Close the stream
controller.close();
}
3. Timers and Periodic Tasks
Timer
: Executes a callback after a specified duration.Timer.periodic
: Executes a callback periodically at a specified interval.
Example - Using Timer
for Delayed Execution:
import 'dart:async';
void main() {
print('Start');
Timer(Duration(seconds: 3), () {
print('Printed after 3 seconds');
});
print('End');
}
Classes
- Completer<T>
A way to produce Future objects and to complete them later with a value or error.
- EventSink<T>
A Sink that supports adding errors.
- Future<T>
The result of an asynchronous computation.
- FutureOr<T>
A type representing values that are either Future<T>
or T
.
An enhanced stream controller provided by
A source of asynchronous data events.
Abstract interface for a "sink" accepting multiple entire streams.
A controller with the stream it controls.
An Iterator-like interface for the values of a Stream.
- StreamSink<S>
A object that accepts stream events both synchronously and asynchronously.
A subscription on events from a Stream.
- StreamTransformer<S, T>
Transforms a Stream.
- StreamTransformerBase<S, T>
Base class for implementing StreamTransformer.
- StreamView<T>
Stream wrapper that only exposes the Stream interface.
A stream controller that delivers its events synchronously.
A countdown timer that can be configured to fire once or repeatedly.
A zone represents an environment that remains stable across asynchronous calls.
An adapted view of the parent zone.
A parameter object with custom zone function handlers for Zone.fork.
Subscribe to my newsletter
Read articles from Jinali Ghoghari directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by