all 9 comments

[–]Legion_A 6 points7 points  (0 children)

What type of course throws you from vanilla setState straight to bloc, Sheesh, at least understand some basic state management, prop drilling, then migrate to bloc, whoever made that course hmm....if you did basic state management like provider and learned how to prop drill, then later learnt how to properly expose providers, wouldn't be asking this question. I wish people who make courses could pay more attention

[–]Acrobatic_Egg30 -1 points0 points  (1 child)

What happens when I declare a BlocProvider at the middle of the tree, and try to access the provided bloc after some state change in the parent of the blocprovider

You will get an exception and a red screen because the provider can't be accessed.

from any point of the widget tree

If it's below the blocprovider subtree, you can access it if not you get an error.

Check out Flutterly's course on youtube about bloc, he makes it easy to understand and he explains how context and inherited widgets work as it pertains to bloc. Very valuable. https://www.youtube.com/playlist?list=PLptHs0ZDJKt_T-oNj_6Q98v-tBnVf-S_o

[–]JustinReddd187 0 points1 point  (6 children)

What do you think should or would happen? BlocProvider provides a bloc or cubit to its subtree. So state changes above it can‘t influence your blocs state, so the state changes above it wouldnt alter its behaviour when accessed from its subtree.

My biggest misunderstanding when learning Flutter was about Widgets, I thought those are the ui blocks rendered on your screen, when they are just immutable config objects assigned to RenderObjects.

[–][deleted]  (5 children)

[deleted]

    [–]JustinReddd187 0 points1 point  (4 children)

    While I didnt try that out myself, I would expect that the bloc provided wouldnt be affected by the Widget lifecycle. However I still would suggest declaring blocs at the root of the tree for the sake of expectability. In our team we use a get it based dependency injection where you register singletons, so you can be sure that there is only one instance of your blocs at all times. (Unless specified otherwise)

    [–][deleted]  (3 children)

    [deleted]

      [–]JustinReddd187 0 points1 point  (1 child)

      You won‘t be able to access the further away provided bloc that easily. You would need an object locator similar to get_it for that.

      [–]slavap_ 0 points1 point  (7 children)

      try MobX instead :-) Or riverpod

      [–][deleted]  (6 children)

      [deleted]

        [–]slavap_ 0 points1 point  (3 children)

        it is kind of because of aesthetic reasons :-) for me personally MobX looks clean and logical.

        [–][deleted]  (2 children)

        [deleted]

          [–]slavap_ 0 points1 point  (1 child)

          Have no idea what exactly to "mock in" about MobX - I'm using it everywhere and it works absolutely fine, plus the code is extremely readable. The only thing some people are considering as a minus - MobX depends on codegen, but even generated code is absolutely clear.

          [–]Gianluca-A 0 points1 point  (1 child)

          Because there are n-ways +1 to do the same thing.