-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(flutter_bloc): Add dependency to bloc instance for BlocBuilder/BlocListener/BlocConsumer #2482
fix(flutter_bloc): Add dependency to bloc instance for BlocBuilder/BlocListener/BlocConsumer #2482
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2482 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 8 8
Lines 118 139 +21
=========================================
+ Hits 118 139 +21
Continue to review full report at Codecov.
|
@felangel seems like |
@@ -145,6 +155,7 @@ class _BlocBuilderBaseState<B extends BlocBase<S>, S> | |||
|
|||
@override | |||
Widget build(BuildContext context) { | |||
if (widget.bloc == null) context.select<B, int>(identityHashCode); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a very creative idea to solve the previous issue! I made one minor adjustment to use identityHashCode
in case developers override hashCode
manually.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks so much for taking the time to address this issue! I really appreciate the creative solution and will try to get this merged shortly 💙 🙏
Status
READY
Breaking Changes
NO
Description
Fixes #2127
Closes #2181
To make a builder, listener or consumer update when the instance is changed we need a context dependency. However we can't use normal
context.watch
because BlocProvider uses a custom provider which also updates dependents when state is changed, therefore it would makebuildWhen
useless. Ideally we would reverse that, but that would also break compatibility. Adding another "normal" provider is also not an option since provider does not allow it and integrating special InheritedWidget is tricky.There is a concept of "aspect" when it comes to dependencies. Provider exposes it by
context.select
, but it is a bit different and it can't be used indidUpdateDependencies
, but we can put it in build and use it to target only the changes of the instance by retrieving hashCode.I think for people who doesn't change instances in the runtime this makes almost no difference apart from dependency.
Type of Change