Kiwi is a *healthy* dependency injection library for Dart and Flutter
In the mood for something juicy? We have just the thing for you! Meet Kiwi — a dependency injection library for Dart and Flutter. Kiwi aims to be simple, fast, and refreshing! Let’s have a quick look!
It was only a month ago that we experienced the rebirth of the programming language powering Flutter with the release of Dart 2.0.
Today we present to you a new dependency injection library for Dart and Flutter that aims to be just as groundbreaking — yet simple and handy! Meet Romain Rastel’s creation, Kiwi.
Let’s have a look at what Kiwi offers.
It’s more than a fruit
The core of kiwi is the
Container class which implemented as a singleton (a factory that will be called only once, after which the same instance will be provided every time) and this is where all your instances and factories are stored.
The container can store instances, factories, and singletons and it does not rely on reflection, just a
Map, which makes it really fast!
However, it can be agreed that for services with a lot of dependencies, it can be quite tiresome to write that sort of code.
But Kiwi has the answer — a powerful generator to allow the developer to write less code not to mention minimize maintenance costs!
When a dependency is added to a service, you just have to run the generator. You should keep in mind, however, that the generator handles factories and singletons, but not instances.
For more details on how to configure your project to use the generator, you can check the GitHub repo.
You can download Kiwi on pub. The library contains two packages: kiwi, which contains the container and the annotations and kiwi_generator which is the generator. The kiwi package can be used with, or without code generation.
In order to configure, you add
pubspec.yaml under the
dependencies field and in your library, you need to add the following import:
If you would like to have a look behind Kiwi’s creation as well as some examples, you can head over to Romain Rastel’s post.