menu

  1. [ Flutter ](index.html)
  2. dart:async 

dart:async

brightness_4  brightness_5

#  dart:async  library

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

[ Future ](dart-async/Future-class.html) s and [ Stream ](dart-async/Stream-
class.html) s 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 ](dart-async/Timer-class.html) , [ scheduleMicrotask ](dart-
async/scheduleMicrotask.html) and [ Zone ](dart-async/Zone-class.html) .

To use this library in your code:

    
    
    import 'dart:async';
    

##  Future

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 APIs that are
implemented using a different thread or isolate (e.g., the asynchronous I/O
operations of ` dart:io ` or HTTP requests of ` dart:html ` ).

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

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

[ Future.then ](dart-async/Future/then.html) 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 ](dart-async/Future/catchError.html) registers a callback
function that runs if an error occurs within the Future.

##  Stream

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 ](dart-async/Stream/listen.html)
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 ](dart-async/Stream-class.html) , implemented by calling
[ Stream.listen ](dart-async/Stream/listen.html) to get the actual data.

    
    
    Stream<List<int>> stream = File('quotes.txt').openRead();
    stream.transform(utf8.decoder).forEach(print);
    

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

  * The [ dart:async section of the library tour ](https://dart.dev/guides/libraries/library-tour#dartasync---asynchronous-programming) : A brief overview of asynchronous programming. 

  * [ Use Future-Based APIs ](https://dart.dev/codelabs/async-await) : A closer look at Futures and how to use them to write asynchronous Dart code. 

  * [ Futures and Error Handling ](https://dart.dev/guides/libraries/futures-error-handling) : Everything you wanted to know about handling errors and exceptions when working with Futures (but were afraid to ask). 

  * [ The Event Loop a