Injector class Null safety

A simple injector implementation for use in Flutter projects where conventional reflection (mirrors) is not available.

import 'package:flutter_simple_dependency_injection/injector.dart';

void main() {
  final injector = Injector();
  injector.map<Logger>((i) => Logger(), isSingleton: true);
  injector.map<String>((i) => 'https://api.com/, key: 'apiUrl');
  injector.map<SomeService>((i) => SomeService(i.get(Logger), i.get(String, 'apiUrl')));

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

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

Constructors

Injector([String name = DEFAULT_INJECTOR_NAME])
Get the instance of the named injector creating an Injector instance if the named injector cannot be found.
factory

Properties

hashCode int
The hash code for this object.
read-onlyinherited
name String
The name of this injector.
final
runtimeType Type
A representation of the runtime type of the object.
read-onlyinherited

Methods

dispose() → void
Disposes of the injector instance and removes it from the named collection of injectors
get<T>({String? key, Map<String, dynamic>? additionalParameters}) → T
Gets an instance of the given type of T and optional given key and parameters.
getAll<T>({Map<String, dynamic>? additionalParameters}) Iterable<T>
Gets all the mapped instances of the given type and additional parameters
isMapped<T>({String? key}) bool
Returns true if the given type has been mapped. Optionally give it a named key.
map<T>(ObjectFactoryFn<T> factoryFn, {bool isSingleton = false, String? key, bool allowFutureReassignment = false}) Injector
Maps the given type to the given factory function. Optionally specify the type as a singleton and give it a named key.
mapWithParams<T>(ObjectFactoryWithParamsFn<T> factoryFn, {String? key, bool allowFutureReassignment = false}) Injector
Maps the given type to the given factory function. Optionally give it a named key.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed.
inherited
removeAllMappings<T>() Injector
Removes all the mappings for the given type The remove operation is silent, means no exception is thrown if the type or key combination is not present.
removeMapping<T>({String? key}) Injector
Removes the type mapping from the injector if present. Optionally give it a named key. The remove operation is silent, means no exception is thrown if the type or key combination is not present.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited