0

If the following setIntVal is called and notifyListeners(); is executed, is the Text redrawn? intVal is changed but strVal is not.

Similarly, what happens when StateNotifier is used?

class DummyManager with ChangeNotifier {
  DummyManager();

  int intVal = 0;
  String strVal = "";

  void setIntVal(val) {
    intVal = val;
    notifyListeners();
  }
}

Consumer<DummyManager>(
    builder: (context, model, child) {
      return Text(model.strVal);
    },
)
Ganessa
  • 782
  • 2
  • 7
  • 24

3 Answers3

2

Yes. By default, any change in a notifier rebuilds a consumer.

But you can optimize this, using select:
By changing your consumer to:

Consumer(
  builder: (context, ref, child) {
    final strVal = ref.watch(modelProvider.select((v) => v.strVal));
    return Text(strVal);
  },
)

Then your widget will rebuild only when that specific value changes.

Do note that the result is expected to be immutable. As such, avoid retuning lists

Rémi Rousselet
  • 256,336
  • 79
  • 519
  • 432
1

yes of course redrawn, and notify all listeners and by the end consumer have a listener to do this

  • Ah. I see. We'll have to break up the ChangeNotifier and StateNotifier into smaller pieces to minimize redraws. – Ganessa Oct 25 '22 at 06:53
1

You modify your state object in your StateNotifier class and the interface is redrawn. Just have to consider that the new state must be a new object and immutable

Ruble
  • 2,589
  • 3
  • 6
  • 29
  • So if we update the state object in the StateNotifier class, it will be redrawn? I understand. – Ganessa Oct 25 '22 at 06:51
  • Yes. And in this class you can put additional logic and many-many methods. Everything that is responsible for the state will be in this class. – Ruble Oct 25 '22 at 06:55