BlocProvider

Screen.BlocProvider

@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 to its child composable content and all the associated composable tree. The bloc can be retrieved by calls to 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 because the lifecycle of the provided Bloc is bound to the the lifecycle of that Screen (similar to 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

in the original flutter_bloc implementation there is an option to create the provided bloc lazily.

There is currently no such option in this implementation

Screen.BlocProviderForTag

same as BlocProviderwith 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

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

Same as 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 it is associated with, not here.

In flutter_bloc this method was called BlocProvider.value

Screen.BlocProviderExtForTag

same as BlocProviderExt but with an additional blockTag parameter

rememberProvidedBloc

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

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.

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

Last updated