zone_di 1.0.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 50

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();

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.0.0 #

Initial release.


/// Example of how a server side framework might use this package to pass
/// configuration and dependencies around via dependency injection.
/// Class names borrowed from [Aqueduct](
library zone_di.example;

import 'package:zone_di/zone_di.dart';

// Persistence layer interface

final persistentStoreToken =

abstract class PersistentStore {}

// Postgres implementation of persistence layer

final databaseCredentialsToken =

class DatabaseConfiguration {
  DatabaseConfiguration(this.username, this.password);
  final String username;
  final String password;

class PostgreSQLPersistentStore implements PersistentStore {
  PostgreSQLPersistentStore() {
    final credentials = inject(databaseCredentialsToken);
    connect(credentials.username, credentials.password);

  void connect(String username, String password) {/* ... */}

// Bootstrapping

class App {
  App() : persistentStore = inject(persistentStoreToken);

  final PersistentStore persistentStore;

  void run() {/* ... */}

void main() {
    persistentStoreToken: () => PostgreSQLPersistentStore(),
    databaseCredentialsToken: () =>
        DatabaseConfiguration('pschiffmann', 'dolphins')
  }, () {
    final app = App();;

Use this package as a library

1. Depend on it

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

  zone_di: ^1.0.0

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

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

3. Import it

Now in your Dart code, you can use:

import 'package:zone_di/zone_di.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Jan 16, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
meta ^1.1.7 1.1.8
Dev dependencies
pedantic ^1.8.0
test ^1.6.7