AsyncBuilder<T> class

A Widget that builds depending on the state of a Future or Stream.

AsyncBuilder must be given either a future or stream, not both.

This is similar to FutureBuilder and StreamBuilder but accepts separate callbacks for each state. Just like the built in builders, the future or stream should not be created at build time because it would restart every time the ancestor is rebuilt.

If stream is an rxdart ValueStream with an existing value, that value will be available on the first build. Otherwise when no data is available this builds either waiting if provided, or builder with a null value.

If initial is provided, it is used in place of the value before one is available.

If retain is true, the current value is retained when the stream or future instances change. Otherwise when retain is false or omitted, the value is reset.

If the asynchronous operation completes with an error this builds error. If error is not provided reportError is called with the FlutterErrorDetails.

When stream closes and closed is provided, closed is built with the last value emitted.

If pause is true, the StreamSubscription used to listen to stream is paused.

Example using future:

AsyncBuilder<String>(
  future: myFuture,
  waiting: (context) => Text('Loading...'),
  builder: (context, value) => Text('$value'),
  error: (context, error, stackTrace) => Text('Error! $error'),
)

Example using stream:

AsyncBuilder<String>(
  stream: myStream,
  waiting: (context) => Text('Loading...'),
  builder: (context, value) => Text('$value'),
  error: (context, error, stackTrace) => Text('Error! $error'),
  closed: (context, value) => Text('$value (closed)'),
)
Inheritance

Constructors

AsyncBuilder({Key? key, WidgetBuilder? waiting, required ValueBuilderFn<T> builder, ErrorBuilderFn? error, ValueBuilderFn<T>? closed, Future<T>? future, Stream<T>? stream, T? initial, bool retain = false, bool pause = false, bool? silent, bool keepAlive = false, ErrorReporterFn? reportError})
Creates a widget that builds depending on the state of a Future or Stream.
const

Properties

builder ValueBuilderFn<T>
The default value builder.
final
closed ValueBuilderFn<T>?
The builder that should be called when the stream is closed.
final
error ErrorBuilderFn?
The builder that should be called when an error was thrown by the future or stream.
final
future Future<T>?
If provided, this is the future the widget listens to.
final
hashCode int
The hash code for this object.
no setterinherited
initial → T?
The initial value used before one is available.
final
keepAlive bool
Whether or not we should send a keep alive notification with AutomaticKeepAliveClientMixin.
final
key Key?
Controls how one widget replaces another widget in the tree.
finalinherited
pause bool
Whether or not to pause the stream subscription.
final
reportError ErrorReporterFn
If provided, overrides the function that prints errors to the console.
final
retain bool
Whether or not the current value should be retained when the stream or future instances change.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
silent bool
Whether or not to suppress printing errors to the console.
final
stream Stream<T>?
If provided, this is the stream the widget listens to.
final
waiting WidgetBuilder?
The builder that should be called when no data is available.
final

Methods

createElement() StatefulElement
Creates a StatefulElement to manage this widget's location in the tree.
inherited
createState() State<StatefulWidget>
Creates the mutable state for this widget at a given location in the tree.
override
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
inherited
toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) String
A string representation of this object.
inherited
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a string representation of this node and its descendants.
inherited
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a one-line detailed description of the object.
inherited
toStringShort() String
A short, textual description of this widget.
inherited

Operators

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