Provider: Add context.selectChange

Created on 31 Mar 2020  路  1Comment  路  Source: rrousselGit/provider

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();
});
enhancement

Most helpful comment

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.

>All comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mhd-barikhan picture mhd-barikhan  路  5Comments

hou3172568 picture hou3172568  路  4Comments

Alfie-AD picture Alfie-AD  路  4Comments

FaizanKamal7 picture FaizanKamal7  路  6Comments

dikir picture dikir  路  5Comments