kiwi 0.1.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 91

kiwi

Pub Donate

Logo

A simple yet efficient IoC container for Dart and Flutter.

The container does not rely on reflection, it's just a Map, so it's fast.

IMPORTANT: Dart2 is required to use this package.

This package can be used with, or without code generation. While code generation allows you to code faster, it comes with extra configuration on you side (to be setup only one time). This section is only about kiwi which contains the IoC container and the annotations. If you are looking for the kiwi_generator configuration, you can find documentation here.

Configuration

Add kiwi to pubspec.yaml under the dependencies field. The latest version is Pub

dependencies:
  kiwi: ^latest_version

Import

In your library add the following import:

import 'package:kiwi/kiwi.dart';

Usage

The core of kiwi is the Container class. This is where all your instances and factories are stored. The Container is implemented as a singleton, you can access the single instance like this:

Container container = Container();

Note: I promise you, even if this is looking like a constructor, you will always end up with the same instance 😉.

It works like a lot of IoC containers: you can register a factory under a type, and then resolve the type to get a value.

Registering

You can register 3 kinds of objects:

Instances

Kiwi can register simple instances like that:

container.registerInstance(Sith('Anakin', 'Skywalker'));

You can also give a name to a specific instance:

container.registerInstance(Sith('Anakin', 'Skywalker'), name: 'DartVader');

By default instances are registered under their type. If you want to register an instance under a supertype, you have to specify both of them:

container.registerInstance<Character, Sith>(Sith('Anakin', 'Skywalker'), name: 'DartVader');

In the above example Character is a supertype of Sith.

Factories

container.registerFactory((c) => Sith('Anakin', 'Skywalker'));

You can also give a name to a specific factory:

container.registerFactory((c) => Sith('Anakin', 'Skywalker'), name: 'DartVader');

By default factories are registered under the return type of the factory. If you want to register an factory under a supertype, you have to specify both of them:

container.registerFactory<Character, Sith>((c) => Sith('Anakin', 'Skywalker'), name: 'DartVader');

Note: the c parameter is the instance of the Container, we will saw later how it can be useful.

Singletons

Singletons are registered like factories but they are called only once: the first time we get their value.

container.registerSingleton((c) => Sith('Anakin', 'Skywalker'));

Resolving

You can get the instance registered for a type like this:

Sith theSith = container.resolve<Sith>();

If it was registered under a name, you can get its value like this:

Sith theSith = container.resolve<Sith>('DartVader');

The Container is a callable class. You can also resolve a type like that:

Sith theSith = container<Sith>('DartVader');

Usage with dependencies

If you have a service that depends on another, you have to add the dependency in the constructor. For registering the service, you can then use the c parameter we saw earlier to resolve the value.

class Service {}

class ServiceA extends Service {}

class ServiceB extends Service {
  ServiceB(ServiceA serviceA);
}
...
// Registers a complex factory by resolving the dependency
// when the type is resolved.
Container container = Container();
container.registerFactory((c) => ServiceB(c.resolve<ServiceA>()));

For services with a lot of dependencies, it can be tedious to write that sort of code. That's why kiwi comes with a generator 😃!

Unregistering

You can unregister a factory/instance at any time:

// Unregisters the Sith type.
container.unregister<Sith>();

// Unregister the Sith type that was registered under the name DartVader.
container.unregister<Sith>('DartVader');

Cleaning

You can remove all the registered types by calling the clear method:

container.clear();

Ignoring assertion errors in development mode

By default kiwi throws an AssertionError in the following cases:

  • if you register the same type under the same name a second time.
  • if you try to resolve or unregister a type that was not previously registered.

This helps you to prevent potential errors in production, however you might want to ignore these assertion errors. To do this you can set true to the silent property of the Container:

container.silent = true;

In production, or when silent is true, you will get null if you try to resolve a type that was not previously registered.

Changelog

Please see the Changelog page to know what's recently changed.

0.1.0

  • Initial Open Source release.

example/kiwi_example.dart

import 'package:kiwi/kiwi.dart';

main() {
  Container container = Container();
  container.registerInstance(Logger());
  container.registerSingleton((c) => Logger(), name: 'logA');
  container.registerFactory((c) => ServiceA(c.resolve<Logger>('logA')));
}

class Service {}

class ServiceA extends Service {
  ServiceA(Logger logger);
}

class Logger {}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  kiwi: ^0.1.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:kiwi/kiwi.dart';
  
Version Uploaded Documentation Archive
0.1.0 Sep 9, 2018 Go to the documentation of kiwi 0.1.0 Download kiwi 0.1.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
82
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
91
Learn more about scoring.

We analyzed this package on Nov 14, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.12.6

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:kiwi/kiwi.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
Dev dependencies
test ^1.0.0