I know that some of my BLOCs will be instantiated only once throughout the app lifecycle. Is it bad practice or anti-pattern to create these BLOC classes as a singleton. Singleton can help in accessing the BLOC anywhere and also makes sure that the class is instantiated only once.
I can also ensure to be safe when using one BLOC inside another BLOC and passing around the singleton as if it were "just another" implementation.
What might be the effects of doing so?
I have two Blocs
The AuthenTicationBloc has to use the UserBloc inside it. If I made UserBloc a singleton , it would make it easy to just use it as UserBloc()
.
Othewise I had to create UserBloc as an instance variable in AuthenTicationBloc as follows:
class AuthenTicationBloc {
UserBloc _userBloc;
AuthenTicationBloc({@required UserBloc userBloc}) : _userBloc = userBloc;
myMethod(){
//use the userBloc
_userBloc.setDetails();
}
}
runApp(
UserBloc userBloc = UserBloc();
Provider(
value: AuthenTicationBloc(userBloc: userBloc),
child: MyApp(),
),
);
If I had to use the UserBloc down the widgetchain somewhere else I had to make the UserBloc also be provided globally as follows right?
UserBloc userBloc = UserBloc();
MultiProvider(
providers: [
Provider<AuthenTicationBloc>(
value: AuthenTicationBloc(userBloc:userBloc),
),
Provider<UserBloc>(
value: userBloc,
),
],
child: MyApp(),
);
Bloc is a good pattern that will be suitable for almost all types of apps. It helps improve the code's quality and makes handling states in the app much more manageable. It might be challenging for someone who is just beginning to use Flutter because it uses advanced techniques like Stream and Reactive Programming.
Save this answer. Show activity on this post. Static objects are singletons, but assigning an object into a static variable isn't the only way to deal with singletons.
The singleton pattern is a software design pattern that restricts the instantiation of a class to one "single" instance. The page also says that the singleton pattern solves problems by allowing it to: Ensure that a class only has one instance. Easily access the sole instance of a class.
The most popular approach is to implement a Singleton by creating a regular class and making sure it has: A private constructor. A static field containing its only instance. A static factory method for obtaining the instance.
The behavior stays the same. A singleton vs InheritedWidget doesn't change much
But it has a few architectural consequences, as you should use an InheritedWidget for BLoCs that aren't "instantiated only once throughout the app lifecycle."
Which means that:
On another note, singletons do not prevent some mistakes that InheritedWidgets do.
For example, it is impossible to have a circular dependency using InheritedWidgets, when it is definitely possible using singletons.
You're not winning much by using a Singleton vs InheritedWidget in term of code either.
There's a good chunk of libraries on Pub to make your job easier. Including provider:
runApp(
Provider(
value: MyBloc(),
child: MyApp(),
),
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With