Getting Started with Flutter application

This tutorial lets you write an Flutter/Dart application and use Koin inject and retrieve your components.


Add the Koin Flutter dependency like below:

koinpub package
koin_flutterpub package
koin: ^[laste_version]
koin_flutter: ^[laste_version]

Our components

Let's create a HelloRepository to provide some data:

class HelloRepository {
giveHello() => "Hello Koin";

Let's create business logic class, for consuming this data:

class MyBloc {
final HelloRepository helloRepository;

Writing the Koin module

Use the module function to declare a module. Let's declare our first component:

// just declare it
final myModule = Module()
..single((s) => HelloRepository())
..factory((s) => MyBloc(s.get()));

?> 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:

class MyApp extends StatefulWidget {
_MyAppState createState() => _MyAppState();
class _MyAppState extends State<MyApp> {
void initState() {
startKoin((app) {
app.printLogger(level: Level.debug);
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
home: MyHomePage());

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:

class MyHomePage extends StatelessWidget {
Widget build(BuildContext context) {
// Get a dependency
var controller = get<MyBloc>();
return Container(
child: Text("${controller.toString()}"),

The by inject() function allows us to retrieve lazy Koin instances The get() function is here to retrieve directly an instance (non lazy)