Announcing kiwi

Dependency injection for Dart and Flutter

Today, I’m very pleased to announce kiwi, a dependency injection library for Dart and Flutter which aims to be simple and fast.

Why?

Even if I’m writing a small application, I tend to separate the logic from the UI. Because projects grow, I think it’s a good practice to do this if the project is not a proof of concept.

I like to have small services with one responsibility. It’s easier to test, to maintain, to read, to understand. A problem with this principle is that you end up with services having a lot of dependencies. Creating such a service can be tedious if you have to instantiate all the dependencies before calling the constructor. In most of the cases, you can rely on IoC libraries to do this for you and most of them use reflection at runtime to create an object.

But since Flutter cannot run code that depends on dart:mirrors , we cannot use Dart libraries that lies on reflection to create objects.

So I looked for IoC libraries that we can use in Flutter. Here are the 3 most promising libraries (for me) and why I didn’t use them:

inject: The package is offered as-is, and I found it too complicated for my needs (I never used Dagger).

get_it: It’s a really good Service Locator library, but it does not provide a generator in case we want to use it for constructor injection.

dioc: This is the one that almost fit my needs, but it didn’t support Dart 2 at the time I started my own package. Some other features I wanted were not implemented either.

After this little research, I found the need to create my own library (which is heavily inspired by dioc and get_it) and I ended up sharing it with you now 🙂.