injectorio 1.0.4 copy "injectorio: ^1.0.4" to clipboard
injectorio: ^1.0.4 copied to clipboard

outdated

Inject your dependencies easily and quickly. Register in one place and use `get()` everywhere to retrieve your instances and Keeper will take car of the rest.

InjectorIO - Dependency Injection for Flutter #

Pub support

Inject your dependencies easily and quickly. Register in one place and use get() everywhere to retrieve your instances and InjectorIO will take care of the rest.

Features #

  • ✅ Create singleton instances;
  • ✅ Create factory instances (recreated on every call);
  • ✅ Register instances using Module;
  • ✅ Get instances from anywhere using the get() function.
  • ✅ Logs printed while in DEBUG mode.
  • ✅ Easy to test.
  • ✅ Doesn't use reflection.
  • ✅ InjectorIO prevents you from keeping instances of classes that extends Widget.

Core concepts #

  • get() => Used to resolve the instance of a registered class. This is what you will use most.
  • inject() => Used to resolve a dependency inside a Module.
  • single() => Used to register a singleton instance. You will receive a the same instance every time you use get().
  • factory() => Used to register a factory instance. You will receive a new instance every time you use get().

NOTE: don't get confused with get() and inject(). Just remember this: If you are inside a Module and you want to resolve a dependency use inject(), but if you are not within a Module always use get().

Usage #

Basic Sample #

Here is how you can easily use this package. Import this package and register your dependency instance, then in any part of your code use get() to resolve the registered instance.


import 'package:injectorio/injectorio.dart';

void main(){
  InjectorIO.start()
  .single( CountriesRepository()); // register a instance

  runApp(MyApp());
}

class _MyHomePageState extends State<MyHomePage> {
  // This works
  // final CountriesRepository repository = get();

  CountriesRepository _repository;

  @override
  void initState() {
    super.initState();
    _repository = get(); // resolve the dependency
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

Register Dependencies #


import 'package:injectorio/injectorio.dart';

void main() {
  InjectorIO.start()
  .single(CountriesWebService())
  .factory(() => CountriesRepository(get()));

  runApp(MyApp());
}

Register Dependencies using Module #


import 'package:injectorio/injectorio.dart';

class CountriesWebService{}

class CountriesRepository{
  final CountriesWebService webService;
  CountriesRepository(this.webService);
}

class AppModule extends Module{
  AppModule() {
    single( CountriesWebService()); // register a singleton of CountriesWebService
    factory(CountriesRepository(inject())); // the library will take care of getting the instance of CountriesWebService
  }
}

void main(){
  InjectorIO.start()
  .module(AppModule());

  runApp(MyApp());
}

Enable/Disable Logs #

InjectorIO can also provide printed logs while in development mode. The function InjectorIO.start() receives a InjectorMode that can be:

  • ✅ DEBUG - Displays logs
  • ✅ PRODUCTION - Disables logs. You will not see logs from this package in the console.

The default value for this is DEBUG. If you don't want to see logs, just use production mode:

InjectorIO.start(mode: InjectorMode.PRODUCTION)
.module( AppModule());

Help this Library #

You can help/support by:

  • ✅ Reporting a Bug;
  • ✅ Making pull request;
  • ✅ Write a tutorial about this;
  • ✅ ❤️ Staring this repository;
9
likes
40
pub points
53%
popularity

Publisher

unverified uploader

Inject your dependencies easily and quickly. Register in one place and use `get()` everywhere to retrieve your instances and Keeper will take car of the rest.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

flutter, logger

More

Packages that depend on injectorio