felangel / bloc

A predictable state management library that helps implement the BLoC design pattern
https://bloclibrary.dev
MIT License
11.8k stars 3.39k forks source link

RepositoryProvider.create and BlocProvider.create must work like didChangeDependencies #1936

Closed PlugFox closed 2 years ago

PlugFox commented 3 years ago

RepositoryProvider.create and BlocProvider.create must work like didChangeDependencies, not initState. They must depend on their dependencies like InheritedWidget. This is flutter way and reactive data binding.

image image

I use this crutch with bloc library for blocs lifecycle and dependency management: https://gist.github.com/PlugFox/80fe552257546f0b50c7f625324da5bf

PlugFox commented 3 years ago

Problem: https://github.com/PlugFox/bloc_didchangedependencies/blob/bloc_missing_feature/lib/src/home.dart#L14-L31

Fix with provider: https://github.com/PlugFox/bloc_didchangedependencies/blob/provider_fix/lib/src/home.dart#L17-L37

Pretty fix with Stateful widget https://github.com/PlugFox/bloc_didchangedependencies/blob/bloc_lifecycle_widget/lib/src/home.dart#L15-L28

Web demo for understand expected flow: https://plugfox.github.io/didchangedependencies/

image

PlugFox commented 3 years ago

I am currently solving this problem with a widget like this. https://gist.github.com/PlugFox/80fe552257546f0b50c7f625324da5bf But I would like it to be supported by the BlocProvider

rrousselGit commented 3 years ago

As appealing as it may sound, there are numerous limitations around InheritedWidgets that makes this approach dangerous.

This includes:

We can debate whether these scenarios are worse considering. For now, provider takes the easy path and prevents it to avoid potential surprising behaviors.

That's another of the reasons why I made Riverpod. It is able to safely implement such pattern. With Riverpod, those edge-cases do not exist.

felangel commented 3 years ago

@PlugFox can you take a look at https://github.com/PlugFox/bloc_didchangedependencies/pull/1? I believe I was able to achieve the desired behavior. Let me know what you think đź‘Ť

PlugFox commented 3 years ago

@felangel https://github.com/PlugFox/bloc_didchangedependencies/pull/1

This is not a solution of the problem) Do not think about "User" as user. Think it about model and repository dependency may changed over time.

Need to use watch, not read.

This may changed overtime from ui (lifting state) or from data layer. I often see similar problems with other people.

PlugFox commented 3 years ago

@felangel If you need any help, feel free to contact me. I will try to help and explain everything I can.

I don’t have much time and I don’t want to do a pull request without agreeing on plans, as this may go against your views.

Thank you for admitting the problem)

felangel commented 2 years ago

Marking this as won't fix because this behavior is implemented by provider and is intentional as Remi pointed out in https://github.com/felangel/bloc/issues/1936#issuecomment-733760263.