Flutter dependency injection, a true love story

Concept

Dependency injection is the act that provides something that someone needs, example: You have the class Vehicle and this class need one Engine to work properly so, Vehicle depends on Engine. We can provide the Engine with different ways, from constructor of the class, by field injection or by method injection, later we’re going to see some examples. There are several libs and ways to handle dependency injection, but today we’re going to see how get_it does his job!

Get It

This library use the technique of service locator (think of a class that holds all information, and when you need something you call her and get what you need) to provides the dependencies that you need, this is a more simple way to handle but works just fine for your needs.

get_it: x.x.x

Singleton

A singleton is a class that have only one instance through your application, it is like the instance of GetIt, so if you request the instance of class A, in class B and C, both going to have the same instance, you have two ways to declare a singleton with GetIt

First way:

Factory

Factory it is the most used method because when you declare something with factory, each time that you request the instance of some class you’re going to have a new instance of it, so you do not have something holding the instance like a singleton, that’s why we declare using a function () → like lazy Singleton.

Using

Like I said before, we have some different ways to provides the instance of some class, I’m going to show two of them.

Constructor

We’re gonna provides the instance of engine through constructor method for vehicle class.

Field injection

We’re gonna provides the instance of the eletricEngine by field and not by constructor. The getIt variable is a global singleton class, so you can access it from any place. You do not need to specify the interface, like getIt<Engine>() if you have already declared the type of the variable. In dart, it is a good particle to declare the type variables that are global in the class, you can omit in local variables, like the ones inside some method.

Getting seriously

Now that we see how to use GetIt we’re going to use the injectable package to do the job for us. As your project grows, it becomes hard to manage and control all those declarations, so that’s where injectable shine. This package works alongside of GetIt and generate all these declarations using some annotations and build_runner.

dependencies:
injectable: x.x.x
dev_dependencies:
injectable_generator: x.x.x
build_runner: x.x.x
flutter packages pub run build_runner build

Declaring

Important: The code generation works with constructor and field injection types, but if you are using the field type, you still need to use getIt<T>() to recover the instance.

Factory

To declare a factory you just need to use the @injectable annotation and when the code generation finish we're going to have the same code that we right on the examples above.

Singleton

LazySingleton

This follow the example that I said before, if you need something that is not used right away, you can declare using lazy.

Module

Injectable offers one more handful annotation @module when you declare a module, you need to create an abstract class like this:

Named

Using the example that I said before of Vehicle and Engine that have EletricEngine and CombustionEngine we can declare names for different instances of the same interface. Here we have A remoteService interface and two implementations, the default one that should consume some API, and the mock one that access some local JSON. Using the @Named you can declare different instances.

Notes

In flutter, we have several ways and packages to handle dependency injection, I decided to do this post about getIt and injectable, because for me, they are great packages, you can learn easy how to use them, and your code gets easy to understand too, of course if you try to read the generated file, it is a problem but 99% of the time you don’t even know that he exists.

Tips

This post is a compiled text of all the things that I learn using these packages, but you can still access their pages and read the documentation, like injectable you can use environment like dev, prod and provides different instances or, if you have a concise name convention you can use configuration files to generate injection for them without the need to add annotations. I am strong to recommend the reading of the documentation if you want more details.

More from Journal

There are many Black creators doing incredible work in Tech. This collection of resources shines a light on some of us:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rodrigo Martins

Rodrigo Martins

181 Followers

Be brave and boldly go where no man has gone before. Let's discover the future!