Service Locator in Flutter with get_it

Oscar Ivan
3 min readSep 17, 2021
Image is taken from https://pixabay.com/

Let’s start at the beginning as usual.

What is get_it

get_it is a service locator for Dart and Flutter projects. This design pattern allows us to subscribe dependencies and later provide them for client/classes that require.

We can imagine the Service Locator pattern as a provider of services and the classes that use them as clients.

In the above graph we can imagine that the services are created and attached to the ServiceLocator in the setup() method (Saved in cache) and because the ServiceLocator is a singleton class than will be alive as long as the app be alive, so, later when the client requires a service this will be provided.

Demo of recipe app

You can find the entire project in this repo, also you can find the way how the app was built (architecture, libraries) in this previous post.

Include get_it

In pubspec.yaml add under dependencies get_it:^version, in my case ^7.1.3 because of this bug but this will be fixed in future versions I guess.

Finally, synchronize to get the dependencies.

Subscribe instances (Services)

In the lib folder create a folder with name di and under this add a file with the name ServiceLocator.dart, the final structure looks like this:

Package structure after adding ServiceLocator.dart

Now is time to write a little code.

Under ServiceLocator.dart subscribe some dependencies:

  • In line 10 we are creating a final and public instance of GetIt (Service Locator).
  • In line 11 we are creating a function to call when we want to register/subscribe the instances to the service locator.
  • In line 12 we are subscribing an instance of Client which allows us to do http requests to the server.
  • On the other lines, we are also registering other instances always taking into account the order from top to bottom, which means that we can’t subscribe an instance if that requires other instances in its constructor but that instances have not been provided before.

Trigger subscription of instances

Before we can consume the instances\services from the service locator anywhere we need, first, we have to call the function setup() which is in charge to subscribe the instances to the service locator. To do that we have to call that function as soon as the app starts.

Take a look at line 9, which is the place where we call the setup() function.

Consuming instances from Service Locator

Once we have the above steps done we will be able to request the dependencies.

The line to check is the 14 where we retrieve the instance of Client, calling the service locator instance and indicating the type of instance we want.

If you are curious about how the other services are provided check this repo.

Conclusions

Get_it is a library that allows us to use service locator pattern to provide dependencies to our classes.

The difference with dependency injection is that the first (service locator) provides the dependencies through an instance of the service locator other way we can’t access the instances we already defined there.

With dependency injection, the instances are provided to the client (classes) automatically if the instance is available and the client requires them.

That’s it, as usual, if you need to dig in detail into the code feel free to go and check on this repo, and of course, if you have any question or concern just let me know, and finally but not least clap if this information taught you something new.

--

--

Oscar Ivan

Android developer, interested on flutter development and enthusiastic on machine learning. Let’s connect on Linkedin! https://www.linkedin.com/in/oscar-ramirez-