Complete BLoC with Clean Architecture (group chat) Discount !! E-commerce App With Backend Source Code Video and Voice Chatting App Firebase Chatting App Source Code Complete Gym App BLoC State Management Source Code Complete Study App Buy Ticket Booking App Source Code Buy Travel App With Backend Source Code Complete Chat App Udemy Course Special Offer Discount !! Online Learning Course App (BLoC) Online Learning Course App (Riverpod) Online Learning Course App (Getx) Discount !! Shopping App (Provider) Cool Flutter Game Flutter Nodejs Chat App Flutter Nodejs Api And Firebase Chat App Riverpod Task Management App
Flutter BLoC and get_it is a great solution for dependency injection. Make sure you go ahead and install get_it. Once you do it you may create an instance out of it.
See how we get the instance to GetIt and save in a variable called sl
. This instance sl does all the magic. It get the instance of GetIt and call other helper functions. Then we need to register our dependency.
There are three ways to register dependency with GetIt.
In general the next thing we do is to call sl.registerFactory()
method. Inside the method, we have to pass the object that should be registered as dependency.
Let's understand how the basic dependency injection works with GetIt. To inject dependency you need to use one of the above function and mention a class name that needs to injected.
The simplest way doing it is passing your dependency inside an anonymous function. Let's take a look at the example below
Here you see that inside registerFactory we return AuthBloc and AuthBloc is a Bloc class for handing events and emitting new states.
AuthBloc depends on a few other classes like SignIn, SignUp, ForgotPassword and Updateuser class. Each of these classes instances are assigned by calling sl().
sl() would automatically find those instances and assign to AuthBloc.