# BlocProvider

### Screen.BlocProvider

```kotlin
@Composable
inline fun <reified BlocA: BlocBase<*>> Screen.BlocProvider(
    crossinline create: @DisallowComposableCalls (cscope: CoroutineScope) -> BlocA,
    crossinline content:@Composable ()->Unit)
```

`BlocProvider` is a composable which provides a [Bloc](../bloc-and-cubit-overview/bloc) to its child composable `content` and all the associated composable tree. The bloc can be retrieved  by calls to [rememberProvidedBloc ](#rememberprovidedbloc)

It is used as a dependency injection (DI) configuration so that a single instance of a Bloc can be provided to multiple child composables within a subtree. `BlocProvider` is defined as an extension method of [Screen](../../navigator-documentation/navigator-overview/screen) because the lifecycle of the provided Bloc is bound to the the lifecycle of that Screen (similar to [ScreenModel](../../navigator-documentation/navigator-overview/screenmodel)). i.e. when the Screen is disposed so will be all blocs defined with `BlocProvider` in that screen:  the `Bloc.close()` method will be called and associated coroutine scope  `Bloc.cscope` will be canceled

The `create` factory method is used to create the Bloc instance (or retrieving it with some Dependency Injection library). The `cscope` parameter passed to `create` is a `CoroutineScope` bound to the current `Screen` and will be cancelled when the `Screen` is disposed. It is meant to be passed to the Bloc constructor, that always requires such parameter.

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

in the original `flutter_bloc` implementation there is an option to create the provided bloc lazily.&#x20;

There is currently no such option in this implementation

### Screen.BlocProviderForTag

same as  [`BlocProvider`](#screen.blocprovider)with an  additional `blocTag` parameter  to identify a specific bloc instance in case there is more than one instance of a bloc of the same type to be registered to the current composable subtree.

### BlocProvider for externally provided bloc

```kotlin
@Composable
inline fun <reified BlocA: BlocBase<*>> BlocProviderExt(
    externallyProvidedBlock:BlocA,
    crossinline content:@Composable ()->Unit)
```

Same as [Screen.BlocProvider](#screen.blocprovider) but with explicitly specified bloc instance `externallyProvidedBlock` that is not a bloc created and bound to the current screen. Lifecycle of this bloc will be managed in the [Screen ](../../navigator-documentation/navigator-overview/screen)it is associated with, not here.&#x20;

In `flutter_bloc` this method was called `BlocProvider.value`

### Screen.BlocProviderExtForTag

same as `BlocProviderExt` but with an additional `blockTag` parameter



### rememberProvidedBloc

```kotlin
@Composable
inline fun <reified BlocA: BlocBase<*>>
        rememberProvidedBloc(blocTag:String?=null):BlocA?
```

Use this method to obtain a Bloc that was previously configured with `BlocProvider` in a parent composable.

&#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 same type registered for the current composable subtree.

The `blocTag` parameter is not present in the original `flutter_bloc` implementation

In `flutter_bloc` the method was called `BlocProvider.of`.  we have renamed it to reflect the usage of `remember` that is specific to Compose.&#x20;

In `flutter_bloc`, when the requested`Bloc` is not found an exception is thrown. In this implementation instead we return null.
