injector 0.0.1

  • Example
  • Installing
  • Versions
  • new46


The Dependency Injection is more a simple service locator but provides basic functionality.

Use registerDependency<Type>() to register the dependency. This takes a function that gives you an instance of the injector. That way you are able to get other dependency required by your registered dependency.

Get your dependency using `getDependency<Type>()`. 

__Never ever try to register or get dependencies without the generic type! Dart allows it, but we don't ;)__


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

Features and bugs

Please file feature requests and bugs at the issue tracker.


  • 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

main() {
  Injector injector = Injector();

  //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();
    this.database = injector.getDependency<Database>();
    this.customerCar = injector.getDependency<Car>();

Use this package as a library

1. Depend on it

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

  injector: ^0.0.1

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter packages get

Alternatively, your editor might support pub get or flutter packages 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';
Version Uploaded Documentation Archive
0.0.1 Jul 5, 2018 Go to the documentation of injector 0.0.1 Download injector 0.0.1 archive
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 Jul 20, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.69.0
  • pana: 0.11.7


Detected platforms: Flutter, web, other

No platform restriction found in primary library package:injector/injector.dart.

Issues and suggestions

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see

The description is too short.

Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Package is pre-v1 release.

While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction of the API.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.52.0 <2.0.0
meta ^1.1.5 1.1.6
Dev dependencies
test ^0.12.30