flutter_simple_dependency_injection 0.0.3 copy "flutter_simple_dependency_injection: ^0.0.3" to clipboard
flutter_simple_dependency_injection: ^0.0.3 copied to clipboard

outdatedDart 1 only

A super simple dependency injection implementation for flutter that behaviours like any normal IOC container and does not rely on mirrors

flutter_simple_dependency_injection #

A simple dependency injection plugin for Flutter.

This implementation does not rely on the dart reflection apis (mirrors) and favours a simple factory based approach. This increases the performance and simplicity of this implementation.

  • Support for multiple injectors (useful for unit testing or code running in isolates)
  • Support for types and named types
  • Support for singletons
  • Support simple values (useful for configuration parameters like api keys or urls)

Any help is appreciated! Comment, suggestions, issues, PR's!

Getting Started #

In your flutter project add the dependency:

dependencies:
  ...
  flutter_simple_dependency_injection: any

For help getting started with Flutter, view the online documentation.

Usage example #

Import flutter_simple_dependency_injection

import 'package:flutter_simple_dependency_injection/injector.dart';

Injector Configuration #

As this injector relies on factories rather than reflection (as mirrors in not available in Flutter) each mapped type needs to provide a factory function. In most cases this can just be a simple new object returned function. In slightly more advanced scenarios where the type to be created relies on other types an injector instances is passed into the factory function to allow the type of be created to get other types it depends on (see below for examples).

import 'package:flutter_simple_dependency_injection/injector.dart';

void main() {
  final injector = Injector.getInjector();
  injector.map<Logger>((i) => new Logger(), isSingleton: true);
  injector.map<String>((i) => "https://api.com/", key: "apiUrl");
  injector.map<SomeService>((i) => new SomeService(i.get<Logger>(), i.get<String>("apiUrl")));
  injector.map<SomeGenericType<String>>((i) => new SomeGenericType("Hello"));
  injector.map<SomeGenericType<int>>((i) => new SomeGenericType(42));

  injector.get<SomeService>().doSomething();
  print(injector.get<SomeGenericType<String>>().propertyOfT) // prints "Hello";
  print(injector.get<SomeGenericType<int>>().propertyOfT) // prints 42;
}

class Logger {
  void log(String message) => print(message); 
}

class SomeService {
  final Logger _logger;
  final String _apiUrl;

  SomeService(this._logger, this._apiUrl);

  void doSomething() {
    _logger.log("Doing something with the api at '$_apiUrl'");
  }
}

class SomeGenericType<T> {
  T propertyOfT;
  SomeService(this.propertyOfT);
}

Multiple Injectors #

The Injector class has a static method [getInjector] that by default returns the default instance of the injector. In most cases this will be enough. However, you can pass a name into this method to return another isolated injector that is independent from the default injector. Passing in a new injector name will create the injector if it has not be retrieved before. To destroy isolated injector instances call their [dispose] method.

  final defaultInjector = Injector.getInjector();
  final isolatedInjector = Injector.getInjector("Isolated");
71
likes
0
pub points
91%
popularity

Publisher

unverified uploader

A super simple dependency injection implementation for flutter that behaviours like any normal IOC container and does not rely on mirrors

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter

More

Packages that depend on flutter_simple_dependency_injection