Closed PlugFox closed 2 years ago
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/
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
As appealing as it may sound, there are numerous limitations around InheritedWidgets that makes this approach dangerous.
This includes:
didChangeDependencies
to be called even if the dependencies haven't changed.
This can be an issue for providers that are placed under Navigator, as from memory, pushing routes force-call didChangeDependencies. That would destroy the state of these providersWe 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.
@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 đź‘Ť
@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.
@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)
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.
RepositoryProvider.create and BlocProvider.create must work like didChangeDependencies, not initState. They must depend on their dependencies like InheritedWidget. This is
flutter way
andreactive data binding
.I use this crutch with bloc library for blocs lifecycle and dependency management: https://gist.github.com/PlugFox/80fe552257546f0b50c7f625324da5bf