Replies: 5 comments 3 replies
-
The problem is, aspects are never cleared unless a widget is unmounted. Say you do: context.watch(family(variable)) then when the variable changes, the provider listened to changes – which means a new aspect. But the previous aspect will still be active. So if Same goes for simple conditions, like: Widget build(context) {
if (condition) context.watch(foo);
} When |
Beta Was this translation helpful? Give feedback.
-
That's what We'd have to update Flutter to properly clear aspects when no longer used by Widgets are able to subscribe to an InheritedWidget inside It's highly unlikely that the Flutter team will change something here. |
Beta Was this translation helpful? Give feedback.
-
Got it. I feared there was still something I missed. Thanks for explaining. 👍 |
Beta Was this translation helpful? Give feedback.
-
Just fyi, I've adressed the issues and published it as a new package 'riverpod_context'. |
Beta Was this translation helpful? Give feedback.
-
Thanks and Congratulations to stable v1.0.0🎉 When I want to read or write any provider (and don't want to rebuid), I have used Before v1
From v1
|
Beta Was this translation helpful? Give feedback.
-
Hi. I recognize I'm a bit late to the discussion after reading through #335, but anyways:
When I migrated my projects to riverpod 1.0.0, I was really unhappy about the missing
context.read
extension. It is just such a convenient method, and in my case having a large project with lots and lots of state, I found myself needing to refactor almost every StatelessWidget to a ConsumerWidget, and also many StatefulWidgets. I know there was already a long discussion. For me it's just that I would go and define acontext.read
extension in my projects anyways, so why keep it out of the package?But:
One strong point I totally get is that we would want a similar interface on context that on ref, which means context.watch needs to happen first. Digging through the code, I also understand why it's not easy (any maybe not feasible) to implement this. But being the naive coder I am, I thought "Surely this must be doable somehow? It can't be impossible?".
So here I am coming up with a
context.watch
implementation:https://gist.github.com/schultek/b9d3e10eaf6c62ee741baf49e813febb
It uses
InheritedWidget
(oh my) anddependOnInheritedElement
withaspect: provider
(yikes). Yes it's not really pretty and maybe uses anti-patterns. However it works! On any context, rebuilding only the right widgets. It supports any provider, as well as.autoDispose
and.family
.So I guess my question is if you could see this coming back to riverpod in any way? Do you have feedback on the implementation? I could understand if this is not going to end up in the main package, due to its 'hackiness'. In that case I would probably publish it as a separate package (after some more testing of course). But I wanted to get your thoughts first.
Beta Was this translation helpful? Give feedback.
All reactions