injector 1.0.8

  • Readme
  • Changelog
  • Example
  • Installing
  • 96

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/

Bad: import '../../some/package/

Features and bugs #

Please file feature requests and bugs at the issue tracker.

1.0.8 #

  • Add travis ci badge to readme
  • Reformat using dartfmt

1.0.7 #

  • Refactor docs

1.0.6 #

  • Add remove dependency method
  • Add override dependencies property

1.0.5 #

  • Breaking Changes! Injector is not a singleton. You can use it like a singleton with the static instance Injector.appInstance
  • You can register your dependencies with a name. But be careful, you need always the generic type! It looks like Injector.allInstance.registerDependency<UserService>((injector) => UserServiceImpl), dependencyName: "MyCustomName");

1.0.4 #

  • Add some documentation for each class

1.0.3 #

  • Change license from BSD-3 to Apache 2

1.0.2 #

  • Upgrade analysis_options.yaml
  • Add missing Cast ("as T")
  • Add Test - Cycle dependencies

1.0.1 #

  • Upgrade pubspec.yaml (Change dart sdk constraint)
  • Update
  • Update Description

1.0.0 #

  • Create package,
  • Add Injector class
  • Add unit tests for "Injector"
  • Add example - How to deal with the injector / dependency injector


import 'package:injector/injector.dart';
import 'package:meta/meta.dart';

class Engine {
  String capacity = "";

abstract class Car {
  bool drive();

  bool stop();

class CarImpl extends Car {
  final Engine engine;

  CarImpl({@required this.engine});

  bool drive() {
    return true;

  bool stop() {
    return true;

abstract class Database {
  void add(String text);
  void remove(int id);

class TikkrDatabase extends Database {
  TikkrDatabase() {

  void _initialize(String createTableString) {
    //Create the Database

  String _createTableString() {
    return "CREATE TABLE USER";

  void add(String text) {
    //Do something

  void remove(int id) {
    //Do something

void main() {
  // Use this static instance
  Injector injector = Injector.appInstance;

  //Register a dependency
  injector.registerDependency<Engine>((_) => Engine());

  injector.registerDependency<Car>((Injector injector) {
    var engine = injector.getDependency<Engine>();
    return CarImpl(engine: engine);

  //Maybe you want to register a class and you need it as a singleton
  injector.registerSingleton<Database>((_) => TikkrDatabase());

//Now you can easily get your dependencies / singletons with one line
class WebView {
  Database database;
  Car customerCar;

  WebView() {
    Injector injector = Injector.appInstance;
    database = injector.getDependency<Database>();
    customerCar = injector.getDependency<Car>();

Use this package as a library

1. Depend on it

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

  injector: ^1.0.8

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

  • Dart: 2.7.1
  • pana: 0.13.6


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.20.1 <3.0.0
meta ^1.1.5 1.1.8
Dev dependencies
test ^1.5.2