dart_ioc 1.0.1 copy "dart_ioc: ^1.0.1" to clipboard
dart_ioc: ^1.0.1 copied to clipboard

An IoC container implementation for dart and flutter with support for types, singletons, factory methods and merging of containers.

example/dart_ioc_example.dart

import 'package:dart_ioc/dart_ioc.dart';

abstract class A {
}

class B extends A {
}

main() {

  var containerWithSingletons = Container()
  // register singleton instance
    .registerSingleton('Hello World')
  // register singleton instantiated on first resolve using a factory method.
    .registerSingletonLazy<Stream>(() => Stream.value(100))
  // register singleton instantiated on first resolve using a factory method.
    .registerSingletonLazy<B>();

  String helloWorld = containerWithSingletons.resolve<String>();  
  print(helloWorld); // prints out 'Hello World'

  var containerWithTypes = Container()
  // register type.
    .registerType<B>()
  // register type with factory method.
    .registerType(() => 'Hello World');

  helloWorld = containerWithTypes.resolve<String>();  
  print(helloWorld); // prints out 'Hello World'

  var containerWithBaseTypes = Container()
  // register type as base type.
    .register<A,B>();

  B b = containerWithBaseTypes.resolve<A>();  
  print(b); // prints out 'Instance of 'B''

  // merge containers
  Container container1 = Container().registerType(() => 'Hello World');
  Container container2 = Container().registerType(() => 100);

  print(container1.merge(container2).resolve<int>()); // prints out 100
}
1
likes
40
pub points
0%
popularity

Publisher

unverified uploader

An IoC container implementation for dart and flutter with support for types, singletons, factory methods and merging of containers.

Homepage
Repository (GitLab)
View/report issues

Documentation

Documentation

License

MIT (LICENSE)

More

Packages that depend on dart_ioc