The problem with this:
ExampleView X = ExampleView(bloc,...)
It only make sense if you use your bloc as normal class/provider with some Stream\ValueNotifier
. Otherwise it has some issues.
If it's global bloc, it's more exhausting way to pass it. You should use XBlocProvider
on top of MaterialApp
.
By the way, if it's global/top level bloc, you can do this:
XBlocProvider(
bloc: xBloc, // Singleton
child XPage,
...
This way, you can access this bloc from anywhere of the application and also you can listen
it.
If it's local bloc, because the way we listen Bloc
or ChangeNotifierProvider
via InheritedWidget
's updateShouldNotify
method, it doesn't make sense to pass as constructor
because you can't use it directly as you intended. You need to put that instance inside BlocProvider
and consume it again, so it's extra work.
https://api.flutter.dev/flutter/widgets/InheritedWidget/updateShouldNotify.html
To overcome multi nested BlocProvider
s you can use MultiProvider
or MultiBlocProvider
.
Example:
MultiBlocProvider(
providers: [
XProvider(...),
YProvider(...),
ZProvider(...),
],
child: someWidget,
)
There are multi ways to pass depends on your need but don't worry about InheritedWidget
because it's pretty fast and convenient way to obtain your XBlocProvider
.
At the end, try to comprehend every approach, I especifically suggest you to grasp this article:
https://www.didierboelens.com/2018/12/reactive-programming---streams---bloc---practical-use-cases/
You'll get the idea when to use bloc with provider or as a singleton or instantiate like your example etc.