Using a singleton for Bloc Using a singleton for Bloc dart dart

Using a singleton for Bloc


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:

  • you'll mix InheriedWidgets and Singletons in your app. Reducing consistency as you have two different way to create/use a BLoC
  • it becomes harder to adapt when the specs change. It is very possible that in the future, you may have to instantiate it more than once. That means you'd have to edit the whole application

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(),  ),);