StreamBuilder<T> class

Component that builds itself based on the latest snapshot of interaction with a Stream.

Component rebuilding is scheduled by each interaction, using State.setState, but is otherwise decoupled from the timing of the stream. The builder 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 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 depends on the relative timing of events produced by the stream and the build rate of the Flutter pipeline.

Changing the StreamBuilder 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. 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.

{@tool dartpad} This sample shows a StreamBuilder 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.

** See code in examples/api/lib/components/async/stream_builder.0.dart ** {@end-tool}

See also:

  • ValueListenableBuilder, which wraps a ValueListenable instead of a Stream.
  • StreamBuilderBase, which supports component building based on a computation that spans all interactions made with the stream.
Inheritance

Constructors

StreamBuilder({Key? key, T? initialData, Stream<T>? stream, required AsyncComponentBuilder<T> builder})
Creates a new StreamBuilder that builds itself based on the latest snapshot of interaction with the specified stream and whose build strategy is given by builder.
const

Properties

builder AsyncComponentBuilder<T>
The build strategy currently used by this builder.
final
hashCode int
The hash code for this object.
no setterinherited
initialData → T?
The data that will be used to create the initial snapshot.
final
key Key?
Controls how one component replaces another component in the tree.
finalinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
stream Stream<T>?
The asynchronous computation to which this builder is currently connected, possibly null. When changed, the current summary is updated using afterDisconnected, if the previous stream was not null, followed by afterConnected, if the new stream is not null.
finalinherited

Methods

afterConnected(AsyncSnapshot<T> current) AsyncSnapshot<T>
Returns an updated version of the current summary reflecting that we are now connected to a stream.
override
afterData(AsyncSnapshot<T> current, T data) AsyncSnapshot<T>
Returns an updated version of the current summary following a data event.
override
afterDisconnected(AsyncSnapshot<T> current) AsyncSnapshot<T>
Returns an updated version of the current summary reflecting that we are no longer connected to a stream.
override
afterDone(AsyncSnapshot<T> current) AsyncSnapshot<T>
Returns an updated version of the current summary following stream termination.
override
afterError(AsyncSnapshot<T> current, Object error, StackTrace stackTrace) AsyncSnapshot<T>
Returns an updated version of the current summary following an error with a stack trace.
override
build(BuildContext context, AsyncSnapshot<T> currentSummary) Iterable<Component>
Returns a Component based on the currentSummary.
override
createElement() Element
Creates a StatefulElement to manage this component's location in the tree.
inherited
createState() State<StreamBuilderBase<T, AsyncSnapshot<T>>>
Creates the mutable state for this component at a given location in the tree.
inherited
initial() AsyncSnapshot<T>
Returns the initial summary of stream interaction, typically representing the fact that no interaction has happened at all.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited