Simplifying State Subscription in Flutter with Context-based Listenable Dependency
Use case
Right now, if you have mutable state bound to the widget tree via an inherited widget, you can trivially subscribe to it from anywhere with a valid reference to context:
But this is needlessly opinionated-why can I only subscribe to listenable state if it's held by the widget tree? Lets say I decide to refactor my from an inherited widget to some other state management solution. This is much more verbose and not very isomorphic to the solution:
Add dependencies on a couple more pieces of state and this immediately becomes an unreadable nightmare.
Ideally, my app code would look more or less the same regardless of where I chose to hold my state. I often find myself using provider to bind widget-tree-agnostic listenable state to the widget tree just to avoid verbosity.
Proposal
Provide the following two functions:
Now our example from earlier looks like the following:
This is a little awkward because we reference twice. This looks even cleaner in the case:
I'll admit I don't understand the inner workings of the build cycle well so maybe there are serious gotchas here that I'm not aware of. If so please let me know and I'm happy to try and work through it here!
Can we have specific selector in Listenable like ‘useListenableSelector’ in ‘flutter_hooks’ API look like