Bloc: Question regarding bloc

Created on 24 Oct 2020  路  2Comments  路  Source: felangel/bloc

Hi Felix. Great work man. Thanks for contributing!

I've been using bloc in several projects but I think I'm possibly only scratching its surface. I always use the same pattern which is what you've documented in several places, e.g. https://medium.com/flutter-community/flutter-todos-tutorial-with-flutter-bloc-d9dd833f9df3

Today I like to use a nested bloc. Let me explain.

I have XBloc,

  • which extends Bloc
  • which is used within XWidget
  • which extends Widget

Inside XWidget (widget 1) I have some nesting of widgets which all require / could require XState (state 1) info, I can do so using BlocProvider.of(context).state. This is all within my knowledge and standard use of bloc.

Now comes the question: inside XWidget I want the ability to create another XWidget (widget 2). This other XWidget has another XBloc with its own XState (state 2). If inside this nested XWidget I retrieve BlocProvider.of(context).state, then can I be guarenteed this will be state 2, not state 1?

thanks,

Eliud

question

All 2 comments

Hi @eliudio 馃憢
Thanks for opening an issue and for the positive feedback!

Regarding your question, yes BlocProvider.of will always return the nearest bloc so in the nested case you will always get the second bloc instance 馃憤

Hope that helps!

Thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

craiglabenz picture craiglabenz  路  3Comments

abinvp picture abinvp  路  3Comments

MahdiPishguy picture MahdiPishguy  路  3Comments

1AlexFix1 picture 1AlexFix1  路  3Comments

timtraversy picture timtraversy  路  3Comments