BlocBuilder<B extends StateStreamable<S>, S> constructor

const BlocBuilder<B extends StateStreamable<S>, S>({
  1. required BlocWidgetBuilder<S> builder,
  2. Key? key,
  3. B? bloc,
  4. BlocBuilderCondition<S>? buildWhen,
})

BlocBuilder handles building a widget in response to new states. BlocBuilder is analogous to StreamBuilder but has simplified API to reduce the amount of boilerplate code needed as well as bloc-specific performance improvements. Please refer to BlocListener if you want to "do" anything in response to state changes such as navigation, showing a dialog, etc...

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

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

Only specify the bloc if you wish to provide a bloc that is otherwise not accessible via BlocProvider and the current BuildContext.

BlocBuilder<BlocA, BlocAState>(
  bloc: blocA,
  builder: (context, state) {
  // return widget here based on BlocA's state
  }
)

An optional buildWhen can be implemented for more granular control over how often BlocBuilder rebuilds. buildWhen should only be used for performance optimizations as it provides no security about the state passed to the builder function. buildWhen will be invoked on each bloc state change. buildWhen takes the previous state and current state and must return a bool which determines whether or not the builder function will be invoked. The previous state will be initialized to the state of the bloc when the BlocBuilder is initialized. buildWhen is optional and if omitted, it will default to true.

BlocBuilder<BlocA, BlocAState>(
  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
  }
)

Implementation

const BlocBuilder({
  required this.builder,
  Key? key,
  B? bloc,
  BlocBuilderCondition<S>? buildWhen,
}) : super(key: key, bloc: bloc, buildWhen: buildWhen);