🧊
compose bloc
  • Overview
  • Setup
  • Differences from adrielcafe/voyager
  • Navigator documentation
    • Navigator Overview
      • Screen
      • ScreenModel
      • Navigator
  • Router and path-based navigation
  • Bloc documentation
    • Bloc and Cubit Overview
      • Cubit
      • Bloc
    • Blocs and Compose Overview
      • BlocProvider
      • MultiBlocProvider
      • BlocBuilder
      • BlocSelector
      • BlocListener
      • BlocConsumer
      • SelectorFor
Powered by GitBook
On this page
  • Screen.BlocProvider
  • Screen.BlocProviderForTag
  • BlocProvider for externally provided bloc
  • Screen.BlocProviderExtForTag
  • rememberProvidedBloc

Was this helpful?

  1. Bloc documentation
  2. Blocs and Compose Overview

BlocProvider

PreviousBlocs and Compose OverviewNextMultiBlocProvider

Last updated 1 year ago

Was this helpful?

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 to its child composable content and all the associated composable tree. The bloc can be retrieved by calls to

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 because the lifecycle of the provided Bloc is bound to the the lifecycle of that Screen (similar to ). 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 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

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

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.

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

Bloc
Screen
ScreenModel
rememberProvidedBloc
BlocProvider
Screen
Screen.BlocProvider