Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filtering Consumer updates #26

Closed
aloisdeniel opened this issue Feb 22, 2019 · 8 comments
Closed

Filtering Consumer updates #26

aloisdeniel opened this issue Feb 22, 2019 · 8 comments

Comments

@aloisdeniel
Copy link

aloisdeniel commented Feb 22, 2019

Having a way to filter updates at the consumer level may be useful to fine-grain builds.

For example, if we have a ChangeNotifier model with multiples properties,notifyListeners would be called each time a value of a property changed. If we have a Consumer for that model that only depends on one of the properties, it is updated even when an other property changes.

We could have a bool shouldBuild(T previousValue, T newValue) argument for Consumer for that purpose.

@rrousselGit
Copy link
Owner

How would that be implemented? From my understanding there's no way for widgets to cancel a rebuild

@rrousselGit
Copy link
Owner

I'm going to close this, as it's not doable.

Not only do widgets cannot cancel a rebuild, but there's also a bigger flaw:

In 99% of the situations with ChangeNotifier, previousValue & newValue are actually the same.

@fantasy525
Copy link

@rrousselGit I also need this.

notifyListeners would be called each time a value of a property changed. If we have a Consumer for that model that only depends on one of the properties, it is updated even when an other property changes.

for best performance,I don't think it need to rebuild the entire screen and also I don't want to write a ChangeNotifier for every property.I just need write a ChangeNotifier with many properties.

@fantasy525
Copy link

@aloisdeniel do you have any ideas?

@rrousselGit
Copy link
Owner

That's simply not possible with mutable objects like ChangeNotifier. You need to use an immutable architecture

@fantasy525
Copy link

@rrousselGit can you provide me some examples?and your example is too simple.sorry. i'm confused

@fantasy525
Copy link

I'm going to close this, as it's not doable.

Not only do widgets cannot cancel a rebuild, but there's also a bigger flaw:

In 99% of the situations with ChangeNotifier, previousValue & newValue are actually the same.

I think you can save last value,like Int ,String value.

@rrousselGit
Copy link
Owner

From a discussion on gitter:

Most of the time, the worry you have is just premature optimisation.

But if you really need that optimisation, you can ProxyProvider

class ManyMutableThings with ChangeNotifier {
  String foo;
  int bar;
}

class ImmutableCopyOfTheFewFieldsYouNeed {
  ImmutableCopyOfTheFewFieldsYouNeed({this.foo});

  final String foo;
}

Widget build(BuildContext context) {
  return ProxyProvider<ManyMutableThings, ImmutableCopyOfTheFewFieldsYouNeed>(
    builder: (_, model, __) => ImmutableCopyOfTheFewFieldsYouNeed(foo: model.foo);
    updateShouldNotify: (a, b) => a.foo == b.foo,
    child: Consumer<ImmutableCopyOfTheFewFieldsYouNeed>(
      builder: (_, data) {
        return Text(data.foo);
      },
  );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants