Dart Core Libraries 'dart:async'

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 a Future, 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 a Stream 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

  1. Completer<T>

A way to produce Future objects and to complete them later with a value or error.

  1. EventSink<T>

A Sink that supports adding errors.

  1. Future<T>

The result of an asynchronous computation.

  1. FutureOr<T>

A type representing values that are either Future<T> or T.

  1. MultiStreamController<T>

An enhanced stream controller provided by

  1. Stream.multi.Stream<T>

A source of asynchronous data events.

  1. StreamConsumer<S>

Abstract interface for a "sink" accepting multiple entire streams.

  1. StreamController<T>

A controller with the stream it controls.

  1. StreamIterator<T>

An Iterator-like interface for the values of a Stream.

  1. StreamSink<S>

A object that accepts stream events both synchronously and asynchronously.

  1. StreamSubscription<T>

A subscription on events from a Stream.

  1. StreamTransformer<S, T>

Transforms a Stream.

  1. StreamTransformerBase<S, T>

Base class for implementing StreamTransformer.

  1. StreamView<T>

Stream wrapper that only exposes the Stream interface.

  1. SynchronousStreamController<T>

A stream controller that delivers its events synchronously.

  1. Timer

A countdown timer that can be configured to fire once or repeatedly.

  1. Zone

A zone represents an environment that remains stable across asynchronous calls.

  1. ZoneDelegate

An adapted view of the parent zone.

  1. ZoneSpecification

A parameter object with custom zone function handlers for Zone.fork.