KoinComponent class

KoinComponent interface marker to bring Koin extensions features. Koin is a DSL to help describe your modules & definitions, a container to make definition resolution .What we need now is an API to retrieve our instances outside of the container. That's the goal of Koin components.

Create a Koin Component

To give a class the capacity to use Koin features, we need to tag it with KoinComponent interface. Let's take an example. A module to define MyService instance

class MyService {}
var myModule = module()..single((s) => MyService());

we start Koin before using definition. Initializes koin with startKoin:

void main(vararg args : String){
   // Start Koin
   startKoin((app){
       app.module(myModule);
   });

   // Create MyComponent instance and inject from Koin container
   MyComponent();
}

Here is how we can write our MyComponent to retrieve instances from Koin container.

Use get & by inject to inject MyService instance:

class MyComponent extends View with KoinComponentMixin {
 MyService myService;
 Lazy<MyService> myServiceLazy;

 MyComponent() {
   // lazy inject Koin instance
   myServiceLazy = inject<MyService>();
   // or
   // eager inject Koin instance
   myService = get<MyService>();
 }
}

Unlock the Koin API with KoinComponents

Once you have tagged your class as KoinComponent, you gain access to:

  • inject - lazy evaluated instance from Koin container
  • get - eager fetch instance from Koin container

Retrieving definitions with get & inject

Koin offers two ways of retrieving instances from the Koin container:

  • Lazy<T> t = inject<T>() - lazy evaluated delegated instance
  • T t = get<T>() - eager access for instance
Lazy<MyService> myService = inject();
retrieve directly the instance
MyService myService = get();

The lazy inject form is better to define property that need lazy evaluation.

Resolving instance from its name

If you need you can specify the following parameter with get or inject

  • qualifier - name of the definition (when specified name parameter in your definition) Example of module using definitions names:
class ComponentA {}

class ComponentB {
 final ComponentA componentA;
	///
KoinComponent interface marker to bring Koin extensions features	///  ComponentB(this.componentA);
}
	///
final myModule = module()
 ..single((s) => ComponentA(), qualifier: named("A"))
 ..single((s) => ComponentB(s.get()), qualifier: named("B"));

We can make the following resolutions:

retrieve from given module
var a = get<ComponentA>(named("A"))

No inject() or get() in your API?

If your are using an API and want to use Koin inside it, just tag the desired class with KoinComponent interface.

Mixed in types

Constructors

KoinComponent()

Properties

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

Methods

bind<S, P>([Qualifier qualifier]) → S
Get instance instance from Koin by Primary Type P, as secondary type S @param parameters
inherited
bindWithParam<S, K, P>(P param, {Qualifier qualifier}) → S
Get instance instance from Koin by Primary Type K, as secondary type S @param parameters
inherited
get<T>([Qualifier qualifier]) → T
inherited
getKoin() Koin
Get the associated Koin instance
inherited
getWithParam<T, P>(P param, {Qualifier qualifier}) → T
inherited
inject<T>([Qualifier qualifier]) Lazy<T>
Lazy inject instance from Koin @param qualifier @param parameters
inherited
injectWithParam<T, P>(P param, {Qualifier qualifier}) Lazy<T>
Lazy inject instance from Koin
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

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