Koin.dart

Koin.dart

build codecov Star on Github style: effective dart


A pragmatic lightweight dependency injection framework. This is a port of Koin for Dart projects.

Written in pure Dart, using functional resolution only: no code generation, no reflection, no Flutter context.

Koin is a light container and a pragmatic API

PackagePub
koinpub package
koin_testpub package
koin_flutterpub package
koin_blocpub package
koin_devtoolspub package

Documentation 🚒

What does Koin have and what can I do with it?

  • Allows to dispose your objects at the exact moment that you are no longer using them.

  • Does not depend on the Flutter

  • Combine your state management classes in a simple way.

  • Standard support for the Bloc library, but it can be easily used with any state management.

  • DevTools to inspect the state of your objects.

    • Inspect the internal state of each class at any time on a Flutter page.
  • Dependencies are instances only when needed.

    • Its class is instant when used for the first time. Koin has a implementation of Lazy by Kotlin to provide this functionality.
  • Integration with the life cycle of widgets in Flutter without depending on the context.

  • Very tested

    • Which tests were all ported from the original version, so expect the same internal behavior as the Kotlin version.
    • In addition, Koin has more tests to a high level of test coverage, to ensure the correct internal behavior.
  • It is not invasive.

    • Insert Koin in your project without changing the structure of your Widgets or changing your state management package.
    • Most of your application will not know that Koin exists,so you decrease the coupling and simplify the understanding and make testing easier.
  • As it does not depend on the context at any time you use it in applications that do not depend on Flutter.

What Koin.dart is not?

It is not a state manager. Koin does not have any type of state management, use Koin with your favorite state management package.

Features

  • Pragmatic
  • Modules
  • Scopes
  • Singleton definition
  • Factory definition
  • Scoped definition
  • Support to multiple bindings
  • Support to named definition
  • Easy testing
  • Lazy inject
  • Logging
  • Support to injection parameters
  • Standard support for Bloc library
  • DevTools for state inspection

Quick Start

Setup

Dart

dependencies:
  koin: ^[version]

Flutter

dependencies:
  koin: ^[version]
  koin_fluter: ^[version]

Declare a Koin module

// Given some classes 
class Bloc {
  final Repository service;

  Bloc(this.service);

  get state => "Hello";
}

class Repository {}

// just declare it
var myModule = Module()
  ..single((s) => Bloc(s.get()))
  ..single((s) => Repository());

Starting Koin

Use the startKoin() function to start Koin in your application.

In a Dart app:

void main(List<String> args) {
    startKoin((app){
      app.module(myModule);
    });
  }

In an Flutter app:

void main() {
  startKoin((app) {
    app.printLogger(level: Level.debug);
    app.module(homeModule);
  });
  runApp(MyApp());
}

Injecting dependencies

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    // Get a dependency
    var bloc = get<Bloc>();
    return Container(
      child: Text("${bloc.state()}"),
    );
  }
}

Ask a Question? 🚑

Reporting issues 💥

Found a bug on a specific feature? Open an issue on Github issues

Contribute 🛠

Want to help or share a proposal about Koin? problem on a specific feature?

  • Open an issue to explain the issue you want to solve Open an issue
  • After discussion to validate your ideas, you can open a PR or even a draft PR if the contribution is a big one Current PRs

Maintainers

Credits

Dependencies

Libraries

instace_scope
instance_factory
koin