Flutter
Getting Started with Flutter application
This tutorial lets you write an Flutter/Dart application and use Koin inject and retrieve your components.
Setup
Add the Koin Flutter dependency like below:
Package | Pub |
---|---|
koin | |
koin_flutter |
Our components
Let's create a HelloRepository to provide some data:
Let's create business logic class, for consuming this data:
Writing the Koin module
Use the module
function to declare a module. Let's declare our first component:
?> we declare our MyBloc class as factory
to have a create a new instance each time our Widget will need one.
Start Koin
Now that we have a module, let's start it with Koin. Open your application class. Just call the startKoin()
function:
Injecting dependencies
The MyBloc
component will be created with HelloRepository
instance. To get it into our Widget, let's inject it with the by get()
delegate injector:
The
by inject()
function allows us to retrieve lazy Koin instances Theget()
function is here to retrieve directly an instance (non lazy)