🧊
compose bloc
  • Overview
  • Setup
  • Differences from adrielcafe/voyager
  • Navigator documentation
    • Navigator Overview
      • Screen
      • ScreenModel
      • Navigator
  • Router and path-based navigation
  • Bloc documentation
    • Bloc and Cubit Overview
      • Cubit
      • Bloc
    • Blocs and Compose Overview
      • BlocProvider
      • MultiBlocProvider
      • BlocBuilder
      • BlocSelector
      • BlocListener
      • BlocConsumer
      • SelectorFor
Powered by GitBook
On this page
  • BlocBuilder with automatically retrieved Bloc
  • BlocBuilder for externally provided Bloc

Was this helpful?

  1. Bloc documentation
  2. Blocs and Compose Overview

BlocBuilder

PreviousMultiBlocProviderNextBlocSelector

Last updated 2 years ago

Was this helpful?

BlocBuilder with automatically retrieved Bloc

@Composable
inline fun <reified BlocA:BlocBase<BlocAState>,BlocAState:Any> 
BlocBuilder(
    blocTag:String?=null,
    noinline buildWhen:@DisallowComposableCalls ((previous:BlocAState?,current:BlocAState)->Boolean)?=null,
    content:@Composable (BlocAState)->Unit
    )

BlocBuilder handles retrieving a of the specified type from the registered Blocs in the current composable subtree (see ) and start listening to the associated stream of Bloc state updates, passing it to the content composable method.

Each new value emitted in the Bloc stream of states will trigger recomposition.

Please refer to if you instead want to "do" anything in response to state changes such as navigation, showing a dialog, etc...

An optional blocTag parameter can be specified in order to identify a specific Bloc instance, in case there is more than one instance of a Bloc of the same type registered for the current composable subtree (see ).

The blocTag parameter is not present in the original flutter_bloc implementation

An optional buildWhen lambda can be provided for more granular control over what specific kind of state change should trigger recomposition

buildWhen will be invoked on each Bloc state change: buildWhen takes the previous state and current state and must return a Boolean which determines whether or not the content composable function will be triggered with the new state

For the first call to buildWhen the previous state will be initialized to the state of the Bloc when the BlocBuilder was initialized.

BlocBuilder for externally provided Bloc

@Composable
inline fun <reified BlocA:BlocBase<BlocAState>,BlocAState:Any>
BlocBuilder(
    externallyProvidedBlock:BlocA,
    noinline buildWhen:@DisallowComposableCalls ((previous:BlocAState?,current:BlocAState)->Boolean)?=null,
    content:@Composable (BlocAState)->Unit)

This is the same as the previous method but with an explicitly specified Bloc instance externallyProvidedBlock , not retrieved implicitly from current registered blocs in the current composable subtree.

Use this method if for example you have retrieved the Bloc already with

Bloc
BlocProvider
BlocListener
BlocProvider
rememberProvidedBlocOf