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

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?



Solution 1:[1]

Yes you can add up to 6 consumers and will be as following

    Consumer2<AuthProvider, StorageProvider>(
    builder: (context, authProvider, storageProvider, child) {

    }
    )

Solution 2:[2]

There is another way to get access to your providers: Provider.of<SomeProvider>(context):

Widget build(BuildContext context) {
  final authProvider = Provider.of<AuthProvider>(context);
  final apiProvider = Provider.of<ApiProvider>(context);
  final storageProvider = Provider.of<StorageProvider>(context);

  // Do your usual stuff without wrapping it into Consumer,
  // just pass providers directly to your targets.
  return MyAwesomeWidget(
    authProvider,
    apiProvider,
    storageProvider,
  );
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Islam Emam
Solution 2 Aux