How to use multiple Consumers for a single widget in flutter Provider
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
}
)
Yes you can add up to 6 consumers and will be as following
Consumer2<AuthProvider, StorageProvider>(
builder: (context, authProvider, storageProvider, child) {
}
)
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,
);
}