StateBuilder<S> class

A widget that builds itself based on the latest snapshot of interaction with a StateReadable.

StateBuilder automatically rebuilds whenever the state from its stateReadable changes. It's used for UI components that need to reflect the current state in their presentation.

The builder function is called whenever the state changes and the optional buildWhen condition returns true.

{@tool snippet} This example shows how to use StateBuilder to display a counter:

class CounterWidget extends StatelessWidget {
  final CounterReadable counterReadable;

  const CounterWidget({
    required this.counterReadable,
    super.key,
  });

  @override
  Widget build(BuildContext context) => StateBuilder<int>(
    stateReadable: counterReadable,
    buildWhen: (previous, current) => previous != current, // Rebuild only when the value changes
    builder: (context, count, _) => Text(
      'Count: $count',
      style: TextStyle(fontSize: 24),
    ),
  );
}

class CounterReadable implements StateReadable<int> {
  final StreamController<int> _controller = StreamController<int>.broadcast();
  int _count = 0;

  @override
  int get state => _count;

  @override
  Stream<int> get stream => _controller.stream;

  void increment() {
    _count++;
    _controller.add(_count);
  }

  Future<void> dispose() => _controller.close();
}

{@end-tool}

See also:

  • StateListener, which performs side effects in response to state changes without rebuilding.
  • StateConsumer, which combines both rebuilding and side effects.
  • StateSelector, which rebuilds only when a specific part of the state changes.
Inheritance

Constructors

StateBuilder({required StateReadable<S> stateReadable, required StateWidgetBuilder<S> builder, StateBuilderCondition<S>? buildWhen, Widget? child, Key? key})
Creates a new StateBuilder.
const

Properties

builder StateWidgetBuilder<S>
The builder that builds a widget based on the current state.
final
buildWhen StateBuilderCondition<S>?
Optional condition to determine when the builder should rebuild.
final
child Widget?
The child of the StateBuilder.
final
hashCode int
The hash code for this object.
no setterinherited
key Key?
Controls how one widget replaces another widget in the tree.
finalinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
stateReadable → StateReadable<S>
The source of the state.
final

Methods

createElement() StatefulElement
Creates a StatefulElement to manage this widget's location in the tree.
inherited
createState() State<StateBuilder<S>>
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, 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