flutter_simple_dependency_injection 1.0.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 93

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;

  // get an instace of each of the same mapped types
  injector.map<SomeType>((injector) => new SomeType("0"));
  injector.map<SomeType>((injector) => new SomeType("1"), key: "One");
  injector.map<SomeType>((injector) => new SomeType("2"), key: "Two");
  final instances = injector.getAll<SomeType>();
  print(instances.length); // prints '3'

  // passing in additional arguments when getting an instance
  injector.mapWithParams<SomeType>((i, p) => new SomeType(p["id"]));
  final instance = injector.get<SomeType>(additionalParameters: { "id": "some-id" });
  print(instance.id); // prints 'some-id'
}

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);
}

class SomeType {
  final String id;
  SomeType(this.id);
}

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");

[1.0.0] - 29/10/2018.

  • Updated to version 1.0.0 as api is now stable
  • Updated formatting

[0.0.5] - 26/08/2018.

  • Updated dart version

[0.0.4] - 05/07/2018.

  • Added ability to pass in additional arguments in the factory function with a new method call [mapWithParams].
    final injector = Injector.getInstance();
    injector.mapWithParams<SomeType>((i, p) => new SomeType(p["id"]))
    final instance = injector.get<SomeType>(additionalParameters: { "id": "some-id" });
    print(istance.id) // prints 'some-id'
  • Added ability to get all objects of the same mapped type
    final injector = Injector.getInstance();
    injector.map<SomeType>((injector) => new SomeType("0"));
    injector.map<SomeType>((injector) => new SomeType("1"), key: "One");
    injector.map<SomeType>((injector) => new SomeType("2"), key: "Two");
    final instances = injector.getAll<SomeType>();
    print(instances.length); // prints '3'

[0.0.3] - 01/07/2018.

  • Improved injector interface using generic types instead of a passed in a type to key an object factory

The new api to map and get a type instance

    // the new api
    final injector = Injector.getInstance();
    injector.map<SomeType>((i) => new SomeType())
    final instance = injector.get<SomeType>();

The old api to map and get a type instance

    // the old api
    final injector = Injector.getInstance();
    injector.map(SomeType, (i) => new SomeType())
    final instance = injector.get<SomeType>(SomeType);

[0.0.2] - 28/06/2018.

  • Fixed up linting and file formats

[0.0.1] - 28/06/2018.

  • Initial release

example/example.dart

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>(key: "apiUrl")));

  injector.get<SomeService>().doSomething();

  // get an instace of each of the same mapped types
  injector.map<SomeType>((injector) => new SomeType("0"));
  injector.map<SomeType>((injector) => new SomeType("1"), key: "One");
  injector.map<SomeType>((injector) => new SomeType("2"), key: "Two");
  final instances = injector.getAll<SomeType>();
  print(instances.length); // prints '3'

  // passing in additional arguments when getting an instance
  injector.mapWithParams<SomeType>((i, p) => new SomeType(p["id"]),
      key: "Three");
  final instance =
      injector.get<SomeType>(additionalParameters: {"id": "some-id"});
  print(instance.id); // prints 'some-id'
}

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 SomeType {
  final String id;
  SomeType(this.id);
}

Use this package as a library

1. Depend on it

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


dependencies:
  flutter_simple_dependency_injection: ^1.0.0

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

Alternatively, your editor might support flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_simple_dependency_injection/injector.dart';
  
Version Uploaded Documentation Archive
1.0.0 Oct 29, 2018 Go to the documentation of flutter_simple_dependency_injection 1.0.0 Download flutter_simple_dependency_injection 1.0.0 archive
0.0.5 Aug 25, 2018 Go to the documentation of flutter_simple_dependency_injection 0.0.5 Download flutter_simple_dependency_injection 0.0.5 archive
0.0.4 Jul 5, 2018 Go to the documentation of flutter_simple_dependency_injection 0.0.4 Download flutter_simple_dependency_injection 0.0.4 archive
0.0.3 Jun 30, 2018 Go to the documentation of flutter_simple_dependency_injection 0.0.3 Download flutter_simple_dependency_injection 0.0.3 archive
0.0.2 Jun 28, 2018 Go to the documentation of flutter_simple_dependency_injection 0.0.2 Download flutter_simple_dependency_injection 0.0.2 archive
0.0.1 Jun 28, 2018 Go to the documentation of flutter_simple_dependency_injection 0.0.1 Download flutter_simple_dependency_injection 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
86
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]
93
Learn more about scoring.

We analyzed this package on Nov 14, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.12.6
  • Flutter: 0.11.3

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.35 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11
meta 1.1.6
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test
test any