Skip to content

Proposal for context.read & context.watch #898

@schultek

Description

@schultek

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 a context.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) and dependOnInheritedElement with aspect: 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions