di_container 1.0.2+1

A library for Dart developers.

Created from templates made available by Stagehand under a BSD-style license.

Usage #

A simple usage example:

import 'package:di_container/di_container.dart';

main() {
  /**
   * You don't need assign type,
   * the type will auto map with type return in builder function
   *
   * With example class A and class B
   * the type map with type A because builder return a instance of A
   * the type map with type B because builder return a instance of B
   *
   * But with example abstract class C
   * you must assign type to register like this: Injector.register<C>*
   */

  // Type Provide
  Injector.register(
    type: Type.provide,
    builder: () => A(),
  );
  A a1 = Injector.get();
  A a2 = Injector.get();

  // a1 != a2
  print("A");
  print(a1.hashCode != a2.hashCode);

  // Type Singleton
  Injector.register(
    type: Type.singleton,
    builder: () => B(),
  );
  B b1 = Injector.get();
  B b2 = Injector.get();

  // b1 == b2
  print("B");
  print(b1.hashCode == b2.hashCode);


  // with "name" Key
  Injector.register<C>(
    type: Type.singleton,
    name: "1",
    builder: () => C1(),
  );
  Injector.register<C>(
    type: Type.singleton,
    name: "2",
    builder: () => C2(),
  );

  C c1 = Injector.get(name: "1");
  C c2 = Injector.get(name: "2");

  c1.hello();
  c2.hello();
}

class A {}

class B {}

abstract class C {
  void hello();
}

class C1 implements C {
  @override
  void hello() {
    print("Hello C1");
  }
}

class C2 implements C {
  @override
  void hello() {
    print("Hello C2");
  }
}

Features and bugs #

Please file feature requests and bugs at the issue tracker.

1.0.0 #

  • Initial version, created by Stagehand

1.0.1 #

  • Refactor code.

1.0.2 #

  • Update document.
  • Fixed remove

example/di_container_example.dart

import 'package:di_container/di_container.dart';

main() {
  /**
   * You don't need assign type,
   * the type will auto map with type return in builder function
   *
   * With example class A and class B
   * the type map with type A because builder return a instance of A
   * the type map with type B because builder return a instance of B
   *
   * But with example abstract class C
   * you must assign type to register like this: Injector.register<C>*
   */

  // Type Provide
  Injector.register(
    type: Type.provide,
    builder: () => A(),
  );
  A a1 = Injector.get();
  A a2 = Injector.get();

  // a1 != a2
  print("A");
  print(a1.hashCode != a2.hashCode);

  // Type Singleton
  Injector.register(
    type: Type.singleton,
    builder: () => B(),
  );
  B b1 = Injector.get();
  B b2 = Injector.get();

  // b1 == b2
  print("B");
  print(b1.hashCode == b2.hashCode);


  // with "name" Key
  Injector.register<C>(
    type: Type.singleton,
    name: "1",
    builder: () => C1(),
  );
  Injector.register<C>(
    type: Type.singleton,
    name: "2",
    builder: () => C2(),
  );

  C c1 = Injector.get(name: "1");
  C c2 = Injector.get(name: "2");

  c1.hello();
  c2.hello();
}

class A {}

class B {}

abstract class C {
  void hello();
}

class C1 implements C {
  @override
  void hello() {
    print("Hello C1");
  }
}

class C2 implements C {
  @override
  void hello() {
    print("Hello C2");
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  di_container: ^1.0.2+1

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:di_container/di_container.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
50
Learn more about scoring.

We analyzed this package on Sep 20, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.0
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

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

Health suggestions

Format lib/src/builder.dart.

Run dartfmt to format lib/src/builder.dart.

Format lib/src/injector.dart.

Run dartfmt to format lib/src/injector.dart.

Format lib/src/injector_type.dart.

Run dartfmt to format lib/src/injector_type.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
Dev dependencies
pedantic ^1.0.0
test ^1.0.0