# BlocBuilder

### BlocBuilder with automatically retrieved Bloc

```kotlin
@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](https://beyondeye.gitbook.io/compose-bloc/bloc-documentation/bloc-and-cubit-overview/bloc) of the specified type from the registered `Blocs` in the current composable subtree (see [BlocProvider](https://beyondeye.gitbook.io/compose-bloc/bloc-documentation/blocs-and-compose-overview/blocprovider)) and start listening to the associated stream of Bloc state updates, passing it to the `content` composable method.&#x20;

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

&#x20;Please refer to [BlocListener ](https://beyondeye.gitbook.io/compose-bloc/bloc-documentation/blocs-and-compose-overview/bloclistener)if you instead want to "do" anything in response to `state` changes such as navigation, showing a dialog, etc...&#x20;

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 ](https://beyondeye.gitbook.io/compose-bloc/bloc-documentation/blocs-and-compose-overview/blocprovider)).&#x20;

The `blocTag` parameter is not present in the original `flutter_bloc` implementation&#x20;

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

```kotlin
@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.

&#x20;Use this method if for example you have retrieved the Bloc already with [`rememberProvidedBlocOf` ](https://beyondeye.gitbook.io/compose-bloc/bloc-documentation/blocprovider#rememberprovidedblocof)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://beyondeye.gitbook.io/compose-bloc/bloc-documentation/blocs-and-compose-overview/blocbuilder.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
