Provider: How to avoid unnecessary rebuilds

Created on 24 Aug 2019  路  10Comments  路  Source: rrousselGit/provider

I am using provider in my app, but I faced with unnecessary building.

Example

class AllWidget extends StatelessWidget{

  @override
  Widget build(BuildContext context){
    print('state build called');
    return ChangeNotifierProvider(
            builder: (_) => MyCounter(),
            child: Column(children: <Widget>[
                  MyCounterText(),
                  MyIncreaseButton(),
                  MyDecreaseButton(),
            ]
          ),
    );
  }
}

class MyCounterText extends StatelessWidget{

  @override
  Widget build(BuildContext context) {
    final myCounter = Provider.of<MyCounter>(context);
    print('MyCounterText');
    return Text(myCounter.num.toString());

  }
}

class MyIncreaseButton extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    final myCounter = Provider.of<MyCounter>(context);
    print('MyIncreaseButton');
    return RaisedButton(
      child: Text('Increase +'),
      onPressed: ()=> myCounter.increament(),
    );

  }
}


class MyDecreaseButton extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    final myCounter = Provider.of<MyCounter>(context);
    print('MyDecreaseButton');
    return RaisedButton(
      child: Text('Decrease -'),
      onPressed: ()=> myCounter.decreament(),
    );

  }
}

Now if I click on MyIncreaseButton widget, to inscrease the value, the MyDecreaseButton widget builds too, even when I dont click on it.

And vice versa, if I click on MyDecreaseButton widget, to descrease the value, the MyIncreaseButton widget builds too, even when I dont click on it.

My Expectation is:

When clicking MyIncreaseButton widget, MyDecreaseButton widget should not build.

All 10 comments

See https://github.com/rrousselGit/provider#my-widget-rebuilds-too-often-what-can-i-do

Thank you @rrousselGit, It looks a little hard to me can you please show that solution with my above code?

@muhammadwfa this can help you should_rebuild

@fantasy525
Thank you, I will try and let you

Thank you @rrousselGit, It looks a little hard to me can you please show that solution with my above code?

What don't you understand there?

@rrousselGit why you reacted with thumbs downn emoj?

That's built-in provider. There's no reason to use a 3rd party package.

@rrousselGit Your provider package is great . but why do you think we have no reason to use a third party package . I don't think there's any connection between them , their roles are different . the provider is mainly used for state management , ShouldRebuild is just a Widget , It's just to determine whether you need rebuild widgets or not . You should not restrict us to using your Provider for everything

We're on provider repository here, and the problem described is something that provider tries to solve (Selector)

The answer should not be to use a 3rd party package. The issue should be used to determine what is lacking in the documentation/library to then improve provider.

@rrousselGit I'm glad to see this #221 锛孉ctually, my idea is similar to that锛宻houldUpdate, we really need to customize the condition

Was this page helpful?
0 / 5 - 0 ratings