Which dependency injection in Flutter is the best?

Dependency Injection is not a common thing in Flutter. You may ask “how is it so? I use getIt, flutter_modular or injector!”. Right, those solve the same issue—dependency hell. But those are service locators and not dependency injection libraries. Then which dependency injection in Flutter is the best?

tl;dr None is the best, but injectable is very good.

Dependency Injection in Flutter

What is the difference between Dependency Injection and Service Locator?

In short words—Dependency Injection is handled compile-time while Service Locator runtime. Maybe it seems like a small difference, but it might change a lot in the matter of stability of the app. While Service Locator might not find an instance of a service you try to inject and cause a crash, with Dependency Injection you would not be able to build the app at all. And that’s good! Fail quickly instead of on production!

Another important difference, this time from programmer’s perspective—Dependency Injection is much cleaner! While Service Locator usually has this huge modules with all the instances listed, in Dependency Injection in most cases you just put an annotation. Sometimes you need to create module class to store 3rd party instances, but most of them are handled with annotation.

And what is the difference in the behaviour of Dependency Injection and Service Locator? Service Locator just searches for factories/instances of specified type in modules. Dependency injection generates all the instantiation and injection during compiling with use of annotations.

Service Locator and Dependency Injection libraries for Flutter

What choices do we have when it comes to dependency management in Flutter? Quite a few, but as it is usually in Flutter, many of them are not mature enough. Lets take a look at this short list of possibilities and short descriptions:

  • Service Locator
    • GetIt – quite mature and popular service locator with big user base. Active development.
    • flutter_modular – popular, very often updated. Easy to specify scopes.
  • Dependency Injection
    • Injectable—Built on top of getIt library, generates getIt code with annotations. Dagger alternative in flutter.

That’s it for the libraries that are worth mentioning, the rest of them are either not maintained very well or immature. Immaturity of libraries is number one thing to avoid while choosing flutter library.

What do I use for Dependency Injection in Flutter?

So coming to conclusion, my choice is Injectable. I use it as it makes less mess in the code than any service locator, and it is actually the only real Dependency Injection library out there for Flutter. It is not as mature as I would expect it to be, but it is built on top of GetIt, so I can easily switch to use this library standalone if something goes wrong with Injectable. But that’s not all. Injectable has some really cool features. My favourite one is async dependencies! You don’t need to handle making async instances of your SharedPreferences or Persistence storage. What is more, Injectable is extremely easy to use. Place annotation above declaration of injectable class, init injectable and get it! Injectable annotation tells the library to inject all the needed dependencies and allow class to be injected in other places.

Dependency Injection in Flutter

More about Me and Stonks

If you would like to learn more about me and Stonks, checkout my landing page! Or checkout Article about API handler generator.

Stonks Software House

We make mobile apps. Fast. And also blogging about app development.