menu

  1. [ Flutter ](index.html)
  2. [ widgets.dart ](widgets/widgets-library.html)
  3. StreamBuilder  < T  > class 

StreamBuilder

brightness_4  brightness_5

[ description
](https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/widgets/async.dart#L383
"View source code")

#  StreamBuilder< T  > class

Widget that builds itself based on the latest snapshot of interaction with a [
Stream ](dart-async/Stream-class.html) .

Widget rebuilding is scheduled by each interaction, using [ State.setState
](widgets/State/setState.html) , but is otherwise decoupled from the timing of
the stream. The [ builder ](widgets/StreamBuilder/builder.html) is called at
the discretion of the Flutter pipeline, and will thus receive a timing-
dependent sub-sequence of the snapshots that represent the interaction with
the stream.

As an example, when interacting with a stream producing the integers 0 through
9, the [ builder ](widgets/StreamBuilder/builder.html) may be called with any
ordered sub-sequence of the following snapshots that includes the last one
(the one with ConnectionState.done):

  * ` AsyncSnapshot<int>.withData(ConnectionState.waiting, null) `
  * ` AsyncSnapshot<int>.withData(ConnectionState.active, 0) `
  * ` AsyncSnapshot<int>.withData(ConnectionState.active, 1) `
  * ... 
  * ` AsyncSnapshot<int>.withData(ConnectionState.active, 9) `
  * ` AsyncSnapshot<int>.withData(ConnectionState.done, 9) `

The actual sequence of invocations of the [ builder
](widgets/StreamBuilder/builder.html) depends on the relative timing of events
produced by the stream and the build rate of the Flutter pipeline.

Changing the [ StreamBuilder ](widgets/StreamBuilder-class.html) configuration
to another stream during event generation introduces snapshot pairs of the
form:

  * ` AsyncSnapshot<int>.withData(ConnectionState.none, 5) `
  * ` AsyncSnapshot<int>.withData(ConnectionState.waiting, 5) `

The latter will be produced only when the new stream is non-null, and the
former only when the old stream is non-null.

The stream may produce errors, resulting in snapshots of the form:

  * ` AsyncSnapshot<int>.withError(ConnectionState.active, 'some error', someStackTrace) `

The data and error fields of snapshots produced are only changed when the
state is ` ConnectionState.active ` .

The initial snapshot data can be controlled by specifying [ initialData
](widgets/StreamBuilder/initialData.html) . This should be used to ensure that
the first frame has the expected value, as the builder will always be called
before the stream listener has a chance to be processed.

This sample shows a [ StreamBuilder ](widgets/StreamBuilder-class.html) that
listens to a Stream that emits bids for an auction. Every time the
StreamBuilder receives a bid from the Stream, it will display the price of the
bid below an icon. If the Stream emits an error, the error is displayed below
an error icon. When the Stream finishes emitting bids, the final price is
displayed.

_link_

To create a local project with this code sample, run:  
flutter create --sample=widgets.StreamBuilder.1 mysample

See also:

  * [ ValueListenableBuilder ](widgets/ValueListenableBuilder-class.html) , which wraps a [ ValueListenable ](foundation/ValueListenable-class.html) instead of a [ Stream ](dart-async/Stream-class.html) . 
  * [ StreamBuilderBase ](widgets/StreamBuilderBase-class.html) , which supports widget building based on a computation that spans all interactions made with the stream. 

Inheritance

    

  * [ Object ](dart-core/Object-class.html)
  * [ DiagnosticableTree ](foundation/DiagnosticableTree-class.html)
  * [ Widget ](widgets/Widget-class.html)
  * [ StatefulWidget ](widgets/StatefulWidget-class.html)
  * [ StreamBuilderBase ](widgets/StreamBuilderBase-class.html) < T  ,  [ AsyncSnapshot ](widgets/AsyncSnapshot-class.html) < T  > >
  * StreamBuilder 

##  Constructors

[ StreamBuilder ](widgets/StreamBuilder/StreamBuilder.html) (  {  [ Key
](foundation/Key-class.h