injectorio 1.0.3

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 #

  • [x] Create singleton instances;
  • [X] Create factory instances (recreated on every call);
  • [x] Register instances using Module;
  • [x] Get instances from anywhere using the get() function.
  • [x] Logs printed while in DEBUG mode.
  • [x] Easy to test.
  • [x] Doesn't use reflection.
  • [x] 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:

  • [X] DEBUG - Displays logs
  • [X] 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:

  • [X] Reporting a Bug;
  • [X] Making pull request;
  • [X] Write a tutorial about this;
  • [X] ❤️ Staring this repository;

[1.0.3] - 20/09/2019

  • Removed the required instance definition for factory instances inside a Module

[1.0.2] - 29/08/2019

  • Added logs for debugging purposes and replaced print messages

[1.0.1] - 20/05/2019

  • code formatted

[1.0.0] - 19/05/2019

  • Stable version

[0.1.0] - 19/05/2019

  • Docs updated

[0.0.5] - 15/05/2019

  • Breaking change on the library design

[0.0.4] - 12/05/2019

  • Added Module support

[0.0.3] - 12/05/2019

  • Improved instance query

[0.0.2] - 10/05/2019

  • Added single instances support

[0.0.1] - 10/05/2019

  • First version.

example/README.md

example #

A new Flutter project.

Getting Started #

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Use this package as a library

1. Depend on it

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


dependencies:
  injectorio: ^1.0.3

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support 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:injectorio/injectorio.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
73
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]
86
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
  • Flutter: 1.9.1+hotfix.2

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Format lib/src/bean_registry.dart.

Run flutter format to format lib/src/bean_registry.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
logger ^0.7.0+2 0.7.0+2
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test