BlocBuilder

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 Bloc of the specified type from the registered Blocs in the current composable subtree (see BlocProvider) 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 BlocListener 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 BlocProvider ).

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 rememberProvidedBlocOf

Last updated