dependencies 0.1.5

  • Readme
  • Changelog
  • Example
  • Installing
  • 93

Mentioned in Awesome Dart CircleCI pub package

This is a simple and flexible dependency injection container for dart.

If you are using Flutter consider the dependencies_flutter package.

Features #

  • Immutability: The injector is created via a builder so it won't be modified after being created.
  • Modules: Separate dependencies into logical pars. Useful to separate by environments.
  • Singletons: Bind singletons and lazy singletons.
  • Factories: Bind factories.
  • Named dependencies: Give your dependencies custom names.
  • Extra arguments: Pass arguments to your factory methods.
  • Works with Flutter: Since it doesn't use reflexion it works with Flutter.

Usage #

Set up #

Optionally create a module.

class PlayerModule extends Module {
  void configure(Binder binder) {
      ..bindSingleton("playerkey", name: "api_key")
      ..bindFactory((injector, params) => Player(params["playerId"]))
      ..bindLazySingleton((injector, params) => Rest(injector));

Create a builder and use it to instantiate an injector.

final builder = Injector.builder()
    ..bind(instance: "abc123", isSingleton: true, name: "api_key")
    ..bindSingleton("abc123", name: "api_key_2")
    ..bindFactory((injector, params) => User(params["userId"]))
    ..bindLazySingleton((injector, params) => RestController(params["path"]))

final injector =;

Usage #

Get instances from the container.

final Player player = injector.get(params: {"playerId":1});
final Rest rest = injector.get();
final String apiKey = injector.get(name: "api_key");

Optionally you can manage injectors with the registry. Feel free to create your own or use the singleton.


Injectors can be fetched from the registry.

final injector = InjectorRegistry.instance.get();
final namedInjector = InjectorRegistry.instance.get(name: "named_injector");

0.0.1 #

  • Initial version.

0.0.2 #

  • Added testing.
  • Fixed a bug with registrations.

0.0.3 #

  • Added better binding interfaces for factories and singletons.
  • Added examples.

0.0.4 #

  • Added shortcuts to inject through the InjectorRegistry.
  • Added Flutter version.

0.0.5 #

  • Added configurer and installer.
  • Removed global allow override.

0.1.0 #

  • Added disposing mechanism.
  • Stable API

0.1.1 #

  • Added the possibility to create an injector from bindings.


import 'package:dependencies/dependencies.dart';

void main() {
  final builder = Injector.builder()
    ..bindSingleton("abc123", name: "api_key")
    ..bindLazySingleton((i, p) => Rest(i.get(name: "api_key")))

  final injector = InjectorRegistry.instance.get();

  final player1 = injector.get<Player>(params: Params.fromMap({"id": "1"}));
  final player2 = injector.get<Player>(params: Params.fromMap({"id": "2"}));
  final key = injector.get<String>(name: "api_key");
  final rest = injector.get<Rest>();

class PlayerModule implements Module {
  void configure(Binder binder) {
    binder..bindFactory((i, p) => Player(p.get("id")));

class Player {
  final String id;


  String toString() {
    return 'Player{id: $id}';

class Rest {
  final String key;


  String toString() {
    return 'Rest{key: $key}';

Use this package as a library

1. Depend on it

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

  dependencies: ^0.1.5

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:dependencies/dependencies.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 27, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.4


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
disposables ^0.1.2 0.1.2
quiver ^2.0.1 2.1.2+1
Transitive dependencies
matcher 0.12.6
meta 1.1.8
path 1.6.4
stack_trace 1.9.3
Dev dependencies
test ^1.0.0