Koin for Flutter
Starting Koin for Flutter
In any Flutter Widget:
Flutter Components as KoinComponents
StatefulWidget
, are extended by Koin to be considered as KoinComponent
out of the box:
Those classes can then use:
get()
orby inject()
instance retrievinggetKoin()
to access th currentKoin
instance
If you need to inject dependencies from another class and can't declare it in a module, you can still tag it with KoinComponent
interface.
Extended Scope API
Scope API is more close to the Flutter platform. The StatefulWidget
have extensions for Scope API: currentScope
get the current associated Koin scope.
Koin gives the ScopeStateMixin
mixin already bound to your Flutter StatefulWidget
lifecycle. On dispose()
is calld, it will close automatically. ScopeStateMixin overrides the dispose
method to call the close
method of the current scope.
You can use the ScopeStateMixin
in StatefulWidget
to close automatically the scope when the widget is removed from the tree. Whenever there is a scope associated with a StatefulWidget
you must use the ScopeStateMixin
or manually call the currentScope.close
" function in the dispose
lifecycle widget function.
important
If the ScopeStateMixin
is not used and you do not call currentScope.close
the instantiated scope for each instance of this Widget will not be closed, that is, the StatefulWidget will be removed from the tree, but the scope associated with it will remain in memory.
You can use directly the associated Koin scope to retrieve components:
Easy to declare your Flutter Widgets's scope:
Any StatefulWidget can use directly the scope API: createScope()
, getScope()
and deleteScope()
.