zone_di 1.0.0 copy "zone_di: ^1.0.0" to clipboard
zone_di: ^1.0.0 copied to clipboard

Dependency injection with a functional API that works without code generation, mirrors or passing around Injector objects.

zone_di #

Dependency injection with a functional API that works without code generation, mirrors or passing around Injector objects.

Usage #

The three main exports of this package are Token, provide() and inject(). Use tokens to declare dependencies that can be injected. Then associate values to your tokens with provide(), and read them with inject().

import 'package:zone_di/zone_di.dart';

final fooToken = Token<String>('my_package.fooToken');

class MyConsumer {
  final String foo;

  MyConsumer() : foo = inject(fooToken).toUpperCase();
}

void main() {
  provide(fooToken, 'Hello world', () {
    final consumer = MyConsumer();
    print(consumer.foo);
  });
}

There's also provideFactories() that constructs token values from the given factory functions and handles dependencies between the factories.

How it works #

To make provide() arguments available in inject(), this package uses zones. That means it can avoid global mutable state and the edge cases that typically come with it. For a longer explanation, I wrote a blog post about it here.

1
likes
40
points
23
downloads

Publisher

unverified uploader

Weekly Downloads

Dependency injection with a functional API that works without code generation, mirrors or passing around Injector objects.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

meta

More

Packages that depend on zone_di