Complete Movie App — Dependency Injection(4)
Hi, Glad that you’re here,
We are building a Movie App with the best coding practices and tools out there. In previous articles, Datasources and Repositories & UseCase, we’ve created many classes and instantiated them directly in main.dart.
In this article, we’ll see what is Dependency Injection(DI), why we need DI and how can we introduce DI in Flutter application.
This is a companion article for the Video:
What is Dependency Injection?
Before we understand DI, let’s see what is a Dependency first. In very simple terms, when class A needs class B to perform its operations, then class A is dependent on class B and class B acts as a dependency for class A.
Let’s see what is dependency and what is dependant on our code so far. Look at the below code: https://gist.github.com/3ba5825ef40a36e3d7cf6007e8fdb462
Here are the dependencies and dependants:
ApiClient
depends onClient
MovieRemoteDataSource
depends onApiClient
MovieRepository
depends onMovieRemoteDataSource
GetTrending
depends onMovieRepository
For every usecase call, you’ll have to instantiate all its dependencies like above. This is overhead and a waste of productive hours for any developer. Not only this is a waste of time, but it might also lead to creating some objects multiple times at multiple places which can lead to consuming more memory.
How good it will be if we can rely on some separate dependency provider to provide us with the correct type of dependency whenever required? This dependency provider will also maintain lazy initializations as well as single instances throughout the application.
There are many plugins in Flutter created by the open-source contributors for the exact same purpose. We’ll use the get_it plugin in this series.
GET_IT
Open pubspec.yaml, add the below dependency and run
flutter pub get
command
In the di folder, create a new file get_it.dart
Import get_it
library, and get the static instance of GetIt in a variable:
For the rest of the code in the application, we’ll now use getItInstance
. In our second article/video, we made network calls, so let's start with that first.
Add the below code in the get_it.dart to initialize Client
from http:
<Client>
tells GetIt, what type of object to register.()
is the factory function, that returns the typeClient
=> Client()
actually initialises the data source. This is the way, we tell GetIt what to initialize.registerLazySingleton
will initialize the instance ofClient
when it is first used in the app.
ApiClient
depends on Client
, so let's add that too in get_it.dart:
As, we’ve asked GetIt to initialize
Client
for us, so we rely ongetItInstance()
to provideClient
toApiClient
instance.
MovieRemoteDataSource
depends on ApiClient
, so let's add that in get_it.dart:
Till now, I’ve shown only the registerLazySingleton()
method of GetIt, but there are other methods too. We'll see them in the coming articles. Since the Client
, ApiClient
, and MovieRemoteDataSource
are used throughout the application, so they should have only one instance throughout the application.
Remaining Instances
Let’s declare Repository and UseCases. Open get_it.dart and declare:
https://gist.github.com/ea33d5fb44bc15d1037cd686649d6176
- All UseCases are dependent on
MovieRepository
, that will be resolved by GetIt. MovieRepository
depends onMovieRemoteDataSource
, that will be resolved by GetIt.
We’re done with adding all the objects in get_it.dart, let’s use them in main.dart. Open main.dart and instead of a lot of initializations we did in the previous articles, this time only use GetTrending
from getItInstance
now:
- With the help of the Pedantic package, you can use
unawaited
that will allow the app to not wait for GetIt initialization to happen before launching its first frame. - Import the get_it file that we created.
- Use
unawaited
and call theinit()
method to initialize GetIt - Give the type of instance you need, as we need
<GetTrending>
here.
Now run the app and again there is no difference in the output. You’ll see a list of trending movies in the console.
GetIt injects the dependency required for us. This was all about using GetIt for Dependency Injection. See you in the next part of the series.