dart:async library Null safety

Support for asynchronous programming, with classes such as Future and Stream.

Futures and Streams are the fundamental building blocks of asynchronous programming in Dart. They are supported directly in the language through async and async* functions, and are available to all libraries through the dart:core library.

This library provides further tools for creating, consuming and transforming futures and streams, as well as direct access to other asynchronous primitives like Timer, scheduleMicrotask and Zone.

To use this library in your code:

import 'dart:async';


A Future object represents a computation whose return value might not yet be available. The Future returns the value of the computation when it completes at some time in the future. Futures are often used for potentially lengthy computations such as I/O and interaction with users.

Many methods in the Dart libraries return Futures when performing tasks. For example, when binding an HttpServer to a host and port, the bind() method returns a Future.

 HttpServer.bind('', 4444)
     .then((server) => print('${server.isBroadcast}'))

Future.then registers a callback function that runs when the Future's operation, in this case the bind() method, completes successfully. The value returned by the operation is passed into the callback function. In this example, the bind() method returns the HttpServer object. The callback function prints one of its properties. Future.catchError registers a callback function that runs if an error occurs within the Future.


A Stream provides an asynchronous sequence of data. Examples of data sequences include individual events, like mouse clicks, or sequential chunks of larger data, like multiple byte lists with the contents of a file such as mouse clicks, and a stream of byte lists read from a file. The following example opens a file for reading. Stream.listen registers callback functions that run each time more data is available, an error has occurred, or the stream has finished. Further functionality is provided on Stream, implemented by calling Stream.listen to get the actual data.

Stream<List<int>> stream = File('quotes.txt').openRead();

This stream emits a sequence of lists of bytes. The program must then handle those lists of bytes in some way. Here, the code uses a UTF-8 decoder (provided in the dart:convert library) to convert the sequence of bytes into a sequence of Dart strings.

Another common use of streams is for user-generated events in a web app: The following code listens for mouse clicks on a button.

querySelector('#myButton').onClick.forEach((_) => print('Click.'));

Other resources


A way to produce Future objects and to complete them later with a value or error. [...]
Indicates that loading of libraryName is deferred. [...]
A Sink that supports adding errors. [...]
An object representing a delayed computation. [...]
A type representing values that are either Future<T> or T. [...]
An enhanced stream controller provided by Stream.multi. [...]
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. [...]
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. [...]
Stream wrapper that only exposes the Stream interface.
A stream controller that delivers its events synchronously. [...]
A count-down 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. [...]


Convenience methods on futures. [...]


runZoned<R>(R body(), {Map<Object?, Object?>? zoneValues, ZoneSpecification? zoneSpecification, Function? onError}) → R
Runs body in its own zone. [...]
runZonedGuarded<R>(R body(), void onError(Object error, StackTrace stack), {Map<Object?, Object?>? zoneValues, ZoneSpecification? zoneSpecification}) → R?
Runs body in its own error zone. [...]
scheduleMicrotask(void callback()) → void
Runs a function asynchronously. [...]


ControllerCallback() → void
Type of a stream controller's onListen, onPause and onResume callbacks.
ControllerCancelCallback() FutureOr<void>
Type of stream controller onCancel callbacks.
CreatePeriodicTimerHandler(Zone self, ZoneDelegate parent, Zone zone, Duration period, void f(Timer timer)) Timer
The type of a custom Zone.createPeriodicTimer implementation function. [...]
CreateTimerHandler(Zone self, ZoneDelegate parent, Zone zone, Duration duration, void f()) Timer
The type of a custom Zone.createTimer implementation function. [...]
ErrorCallbackHandler(Zone self, ZoneDelegate parent, Zone zone, Object error, StackTrace? stackTrace) AsyncError?
The type of a custom Zone.errorCallback implementation function. [...]
ForkHandler(Zone self, ZoneDelegate parent, Zone zone, ZoneSpecification? specification, Map<Object?, Object?>? zoneValues) Zone
The type of a custom Zone.fork implementation function. [...]
HandleUncaughtErrorHandler(Zone self, ZoneDelegate parent, Zone zone, Object error, StackTrace stackTrace) → void
The type of a custom Zone.handleUncaughtError implementation function. [...]
PrintHandler(Zone self, ZoneDelegate parent, Zone zone, String line) → void
The type of a custom Zone.print implementation function. [...]
RegisterBinaryCallbackHandler(Zone self, ZoneDelegate parent, Zone zone, R f(T1 arg1, T2 arg2)) ZoneBinaryCallback<R, T1, T2>
The type of a custom Zone.registerBinaryCallback implementation function. [...]
RegisterCallbackHandler(Zone self, ZoneDelegate parent, Zone zone, R f()) ZoneCallback<R>
The type of a custom Zone.registerCallback implementation function. [...]
RegisterUnaryCallbackHandler(Zone self, ZoneDelegate parent, Zone zone, R f(T arg)) ZoneUnaryCallback<R, T>
The type of a custom Zone.registerUnaryCallback implementation function. [...]
RunBinaryHandler(Zone self, ZoneDelegate parent, Zone zone, R f(T1 arg1, T2 arg2), T1 arg1, T2 arg2) → R
The type of a custom Zone.runBinary implementation function. [...]
RunHandler(Zone self, ZoneDelegate parent, Zone zone, R f()) → R
The type of a custom Zone.run implementation function. [...]
RunUnaryHandler(Zone self, ZoneDelegate parent, Zone zone, R f(T arg), T arg) → R
The type of a custom Zone.runUnary implementation function. [...]
ScheduleMicrotaskHandler(Zone self, ZoneDelegate parent, Zone zone, void f()) → void
The type of a custom Zone.scheduleMicrotask implementation function. [...]
ZoneBinaryCallback<R, T1, T2>(T1 arg1, T2 arg2) → R
ZoneCallback<R>() → R
ZoneUnaryCallback<R, T>(T arg) → R

Exceptions / Errors

An error and a stack trace. [...]
Thrown when a deferred library fails to load.
Thrown when a scheduled timeout happens while waiting for an async result.