BlocConsumer<B extends BlocBase<S>, S extends Object> class

BlocConsumer exposes a builder and listener in order react to new states. BlocConsumer is analogous to a nested BlocListener and BlocBuilder but reduces the amount of boilerplate needed. BlocConsumer should only be used when it is necessary to both rebuild UI and execute other reactions to state changes in the bloc.

BlocConsumer takes a required BlocWidgetBuilder and BlocWidgetListener and an optional bloc, BlocBuilderCondition, and BlocListenerCondition.

If the bloc parameter is omitted, BlocConsumer will automatically perform a lookup using BlocProvider and the current BuildContext.

BlocConsumer<BlocA, BlocAState>(
  listener: (context, state) {
    // do stuff here based on BlocA's state
  },
  builder: (context, state) {
    // return widget here based on BlocA's state
  }
)

An optional listenWhen and buildWhen can be implemented for more granular control over when listener and builder are called. The listenWhen and buildWhen will be invoked on each bloc state change. They each take the previous state and current state and must return a bool which determines whether or not the builder and/or listener function will be invoked. The previous state will be initialized to the state of the bloc when the BlocConsumer is initialized. listenWhen and buildWhen are optional and if they aren't implemented, they will default to true.

BlocConsumer<BlocA, BlocAState>(
  listenWhen: (previous, current) {
    // return true/false to determine whether or not
    // to invoke listener with state
  },
  listener: (context, state) {
    // do stuff here based on BlocA's state
  },
  buildWhen: (previous, current) {
    // return true/false to determine whether or not
    // to rebuild the widget with state
  },
  builder: (context, state) {
    // return widget here based on BlocA's state
  }
)
Inheritance

Constructors

BlocConsumer({required BlocWidgetListener<S> listener, required BlocWidgetBuilder<S> builder, Key? key, B? bloc, BlocListenerCondition<S>? listenWhen, BlocBuilderCondition<S>? buildWhen})
The BlocConsumer constuctor listen and rebuilds a widget when a bloc state change.
const

Properties

bloc → B?
bloc that has the state. If it's null, it will be infered from BuildContext
finalinherited
builder BlocWidgetBuilder<S>
The builder function which will be invoked on each widget build. The builder takes the BuildContext and current state and must return a widget. This is analogous to the builder function in StreamBuilder.
final
buildWhen BlocBuilderCondition<S>?
Takes the previous state and the current state and is responsible for returning a bool which determines whether or not to call listener of BlocConsumer with the current state.
final
child Widget?
The widget which will be rendered as a descendant.
finalinherited
hashCode int
The hash code for this object.
no setterinherited
key Key?
Controls how one widget replaces another widget in the tree.
finalinherited
listener BlocWidgetListener<S>
Takes the BuildContext along with the bloc state and is responsible for executing in response to state changes.
finalinherited
listenWhen BlocListenerCondition<S>?
Takes the previous state and the current state and is responsible for returning a bool which determines whether or not to call listener with the current state.
finalinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

$use() → S
The $use method is a sugar syntax for the useBloc.
inherited
build(BuildContext context) Widget
Takes the previous state and the current state and is responsible for returning a bool which determines whether or not to trigger builder with the current state.
createElement() → _StatelessHookElement
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
onStateEmitted(BuildContext context, S previous, S state) bool
The onStateEmitted method allows to customize the behavior of the implementation of the BlocWidget
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