Dependency Injection with Flutter

Utsav Ghimire
3 min readJul 25, 2022

--

What is Dependency Injection?

DI is a design pattern in which a class requests dependencies from external sources rather than creating them. It means the dependent objects are injected or supplied to your class. The class doesn’t have to do any initialization by itself. It will get what it wants from the injector.

Loose coupling: When an object gets the object to be used from external sources, we call it loose coupling. In other words, the loose coupling means that the objects are independent.

Tight coupling: When a group of classes is highly dependent on one another, it is called tight coupling.

Dependency injection is one of the most practical design patterns that allow developers to write loosely coupled code. It also helps to keep our code more testable.

Let us take an example,

When some class Jungle needs class Animal to perform its operation, class Jungle is called Dependent, and class Animal is called Dependency.

  • In the above example, our class, Jungle, is tightly coupled. It means the class Jungle is highly dependent on the dependency Animal.
  • If we want to add a new animal Tiger, then there is no way to change the animal to Tiger as we have hardcoded Jungle and Animal classes.

Now using dependency injection to solve our problem.

As we can see, we are injecting the dependency from outside the implementation of the class.

Now we can create as many animals as possible and inject them into the Jungle class.

In this way, our class, Jungle, is loosely coupled.

If we want to know about Lion, create a lion object and inject it into the Jungle class.

Dependency injection is beneficial in Unit Testing because dependencies are injected outside the class. That’s why our class is not any more dependent on other dependencies. And we can easily test our class in isolation.

Constructor as Dependency Injection

Here when Page4 needs helloText, we are passing it through the constructor down to Page4.

It is not good practice as even if Page2 and Page3 don’t require the text, they still have to declare and pass the value. And this is not efficient.

Inherited widget

Inherited Widget as Dependency Injection.

What is Inherited Widget?

In flutter, the inherited widget is a base class that allows those classes to extend the information under the tree from it. It works by informing registered build references when a change occurs.

Now, Inherited Widget helps us access any objects, methods, variables, etc., defined in the Inherited Widget in the widget tree.

But there still is a problem, Inherited Widget needs context to access the object. We can’t inject this dependency inside the class which doesn’t have context.

So that’s a problem now. What if we want to access dependencies inside the classes like DB, Client, etc? There we won't have context, because they're just simple regular classes.

To overcome the problem of the Inherited widget, there is get_it package.

Get it as a dependency injection

Service Locator as a Dependency Injection

Get it is a Service Locator, which purpose is to return the service instances on demand.

The Service Locator pattern is a way to decouple the interface (abstract base class) from a concrete implementation, and at the same time allows accessing the concrete implementation from everywhere in your App over the interface.

To learn more about service locators do visit this site.

Now, using get_it as a Service Locator

We don’t need context or pass the values through the constructors.

Cheers 🍻!!

Please connect with me on Linked in or visit my site

--

--