provider icon indicating copy to clipboard operation
provider copied to clipboard

Add context.selectChange

Open esDotDev opened this issue 4 years ago • 3 comments

I have a use case, where is seems like a combination of read and select would be nice. Or maybe there is another way I'm not aware of.

I basically want to bind a callback when the property on some provided model changes, but I don't want to trigger a rebuild, instead I just want to do something.

In this case, if my open SearchPanel, notices that AppModel.currentPage has changed, I would like the SearchPanel to call close itself.

I'm not sure how to set that up with Provider, short of doing something like this:

  @override
void initState() {
    AppModel m = context.read<AppModel>();
    PageType prevPage = m.currentMainPage;
    m.addListener(() {
      if(m.currentMainPage != prevPage){
        handleClosePressed(false);//Close without submitting
      }
      prevPage = m.currentMainPage;
    });
    super.initState();
}

Something like this would be pretty handy:

void initState() {
    context.selectChange<AppModel, PageType>((m)=>m.currentMainPage, (pt){
       handleClosePressed(false);
   });
   super.initState();
});

esDotDev avatar Mar 30 '20 22:03 esDotDev

This is something that I've been thinking for a while too: Adding a context.onChange<T>.

The biggest issue is how to do that. I don't see how to do such thing without relying on flutter_hooks.

rrousselGit avatar Apr 11 '20 09:04 rrousselGit

@rrousselGit whats the workaround that I am missing on? are there any updates on this feature request?

rshrc avatar Oct 07 '21 12:10 rshrc

As said previously, I have no idea on how to implement this. I'm open to suggestions

rrousselGit avatar Oct 07 '21 12:10 rrousselGit