How to build Consumer Widget of Provider Package Specifically for method of Class in Flutter?

Viewed 151

I want to use Consumer Widget of Provider Package in such Way that it will only run when a specific method of Class run or notifyListeners() call.

Like Here is a class

Class Example with ChangeNotifier{
void exampleMethod(){
//....Do SomeThing
notifyListeners();
  }

void exampleOneMethod(){
//....Do SomeThing
notifyListeners();
  }
}

So when I call Consumer in Some Widget Like This:

  Consumer<Example>(builder:(_,ex,__)=>SomeWidget(
   ex.exampleOneMethod();
      )

Soo Consumer Widget will Only Rebuild if exampleOneMethod run and not on other methods.

1 Answers

The Consumer Widget should be rebuild if some other methods call the notifyListeners() I think that for your purpose you should structure the methods in a different way, or use Provider.of<Example>(context, listen: false).exampleOneMethod() the listend: false should ensure you that it will not rebuild if someone call notify

Related