# BlocListener

### BlocListener with automatically retrieved Bloc

```kotlin
@Composable
inline fun <reified BlocA : BlocBase<BlocAState>, BlocAState : Any>
BlocListener(
    blocTag: String? = null,
    noinline listenWhen: (@DisallowComposableCalls (previousState: BlocAState ?, currentState: BlocAState ) -> Boolean) = null,
    crossinline listener: @DisallowComposableCalls suspend (BlocAState) -> Unit
)
```

`BlocListener` 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.&#x20;

the  `listener` function is invoked in response to `state` changes in the `Bloc`.

&#x20;Note that `listener` is not a Composable function: It is a (potentially) suspendable method, that is invoked with [LaunchedEffect](https://developer.android.com/jetpack/compose/side-effects#launchedeffect).&#x20;

The `listener` is guaranteed to be called only  once for each `state` change,  unlike the `content` method in [BlocBuilder](https://beyondeye.gitbook.io/compose-bloc/bloc-documentation/blocs-and-compose-overview/blocbuilder), that can be triggered because of recomposition.

An optional `blocTag` parameter can be specified in order to identify a specific bloc instance in the case where there is more than one instance of a bloc of 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;

&#x20;An optional `listenWhen` parameter can be provided for more granular control over when `listener` is called.

&#x20;`listenWhen` takes the previous `state` and current `state` and must return a `Boolean` which determines whether or not the `listener` function will be invoked.

For the first call to `listenWhen` the previous `state` will be initialized to the `state` of the `Bloc` when the `BlocListener` was initialized.

### BlocListener for externally provided Bloc

```kotlin
@Composable
inline fun <reified BlocA : BlocBase<BlocAState>, BlocAState : Any>
BlocListener(
    externallyProvidedBlock: BlocA,
    noinline listenWhen: (@DisallowComposableCalls (previousState: BlocAState ?, currentState: BlocAState ) -> Boolean) = null,
    crossinline listener: @DisallowComposableCalls suspend (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/bloclistener.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.
