How to use multiple Consumers for a single widget in flutter Provider

Viktor Vostrikov picture Viktor Vostrikov · Jan 23, 2020 · Viewed 7.2k times · Source

I can't wrap my head around using multiple consumers for a single widget with provider? Suppose my widget is CurvedNavigationBar and I have 4 items in that widget. I also have 4 different classes that extend ChangeNotifier and are responsible for each item in CurvedNavigationBar.

How can I listen to those 4 change notifiers in a single widget? I looked at the documentation and could not found such an example.. is this even possible? I found that Consumer has a builder method, so that means you can build a widget only once/and listen to it once.

Should I rather have a single class that extends ChangeNotifier and then update values in that widget and uses only a single Consumer to listen for updated values?

Answer

Selim Kundakçıoğlu picture Selim Kundakçıoğlu · Jan 24, 2020

There are some other Consumer widgets. Consumer2, Consumer3, Consumer4 till Consumer6. If you want to listen 4 ChangeNotifier you can use Consumer4

Consumer4(
  builder: (context, changeNotifier1, changeNotifier2, changeNotifier3, changeNotifier4, child) {
    // your widget
  }
)