StreamWorker<T> class

A lightweight wrapper around StreamBuilder that provides improved handling of loading, error, and empty states.

This widget streamlines the common pattern of consuming a Stream with built-in loading, error, and empty states. It offers sensible defaults while remaining fully customizable.

Stream Lifecycle Management: The underlying StreamBuilder automatically manages the stream subscription lifecycle. When the widget is disposed, the stream subscription is automatically cancelled. However, if you're using a StreamController, make sure to close it when no longer needed.

Type parameter T is the data type emitted by the stream.

Usage example:

StreamWorker<User>(
  stream: userService.userStream,
  builder: (context, user) => UserCard(user: user),
  errorBuilder: (context, error) => OptionError(
    error: error,
    onRetry: () => setState(() {}),
  ),
  loadingBuilder: (context) => const OptionLoading(),
)
Inheritance
Mixed-in types

Constructors

StreamWorker({Key? key, required Stream<T> stream, required Widget builder(BuildContext context, T data), bool isProd = false, T? initialData, Widget emptyBuilder(BuildContext context)?, Widget errorBuilder(BuildContext context, Object? error)?, Widget loadingBuilder(BuildContext context)?})
Creates a StreamWorker that listens to the provided stream and builds the user interface according to its state.
const

Properties

builder Widget Function(BuildContext context, T data)
Builder invoked when non-null data is available.
final
emptyBuilder Widget Function(BuildContext context)?
Builder used when the data is null or considered empty.
final
errorBuilder Widget Function(BuildContext context, Object? error)?
Builder used when an error occurs.
final
hashCode int
The hash code for this object.
no setterinherited
initialData → T?
Initial data to use while waiting for the first event from stream.
final
isProd bool
Production mode flag.
final
key Key?
Controls how one widget replaces another widget in the tree.
finalinherited
loadingBuilder Widget Function(BuildContext context)?
Builder used while the stream is loading.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
stream Stream<T>
The stream to listen to.
final

Methods

build(BuildContext context) Widget
Builds the UI according to the state of the Stream.
override
buildFromSnapshot(BuildContext context, AsyncSnapshot<T> snapshot) Widget
Builds the appropriate widget based on the snapshot state.
inherited
createElement() StatelessElement
Creates a StatelessElement to manage this widget's location in the tree.
inherited
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, int wrapWidth = 65}) 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