Skip to content
Simple dependency injection for Dart.
Dart
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
example fix_issues Jan 21, 2019
lib feat/travis: Reformat using dartfmt May 10, 2019
test feat/travis: Reformat using dartfmt May 10, 2019
.gitignore remove idea folder Jul 23, 2018
.travis.yml feat/travis: Add travis file May 10, 2019
CHANGELOG.md
LICENSE
README.md Remove white space May 13, 2019
analysis_options.yaml
injector.iml Create Repo Jul 5, 2018
pubspec.yaml feat/travis: Add travis ci badge May 10, 2019

README.md

Injector 💉 Build Status

Injector is a simple dependency injector for Dart.

We have been using this injector in all our projects for quite a while now. It does not replace a complex dependency injection framework like Dagger, but it provides the basics that most apps need. Feature requests are welcomed!

Internally the injector is a singleton that stores instances and builders in a Map.

Use registerDependency<Type>() to register the dependency. Never ever try to register or get dependencies without the generic type! Dart allows it, but we don't ;)

All instances are lazy loaded, meaning that at the time you request the dependency the instance is created. Therefore you are registering a builder function that hands you a injector that you HAVE TO USE when getting child dependencies.

Get your dependency using getDependency<Type>().

Usage

A simple usage example:

// Register a dependency (Every time a new instance)
injector.registerDependency<Car>((injector) {
      var engine = injector.getDependency<Engine>();
      var fuel = injector.getDependency<Fuel>();
      var driver = injector.getDependency<Driver>();
      
      return CarImpl(engine,fuel,driver);
    });
    
// Register a singleton
injector.registerSingleton<Car>((injector) {
          var engine = injector.getDependency<Engine>();
          var fuel = injector.getDependency<Fuel>();
          var driver = injector.getDependency<Driver>();
          
          return CarImpl(engine,fuel,driver);
        });
    
// Register your dependencies / singletons in the  __main.dart__ file

Notice

If you have an issue with getting dependencies from the injector, check that imports of your dependencies classes are absolute.

Good: import 'package:<project_name>/some/package/<file_name>.dart';

Bad: import '../../some/package/<file_name>.dart';

Features and bugs

Please file feature requests and bugs at the issue tracker.

You can’t perform that action at this time.