An pragmatic and flexible lightweight dependency injection library. This is a port of Koin for Dart projects.
Written in pure Dart, using functional resolution only: no code generation, no reflection.
Package | Pub |
---|---|
koin | |
koin_test | |
koin_flutter | |
koin_bloc | |
koin_devtools |
-
Allows to dispose your objects at the moment that you are no longer using them.
-
It does not depend on the Flutter.
- The core does not depend on Flutter, so it is possible to use it with any Dart application.
-
Define in which scope a variable can be accessed.
- The koin scope allows you to define in which part of the widget tree a variable will be accessible
-
Integration by default for Bloc library, but it can be easily used with any state management.
-
Koin DevTools to inspect the state of your objects.
- Inspect the internal state of each object 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 enhance this functionality.
-
It is not invasive.
- Insert Koin in your project without changing the structure of your widgets.
-
Facilitates dependency injection by constructor
- Using dependency injection by constructor you decrease the coupling and make the test easier.
- Makes it easy to know the dependencies of your components. Just look at your class's constructor to identify how dependencies it uses.
- Modules
- Scopes
- Singleton provider(definition)
- Factory provider(definition)
- Scoped provider(definition)
- Support to multiple bindings
- Support to named provider(definition)
- Easy testing
- Lazy inject
- Logging
- Support to parameter injection
- Integration by default for Bloc library
- DevTools for state inspection
It is not a state manager. Koin does not have any type of state management, use koin with any state manager.
- Improve documentation
- Add more examples
- Example of use with Redux, Mobx and RxDart.
- Example with HTTP server frameworks.
- Create an external DevTools
- Add logger plugin for logger
dependencies:
koin: ^[version]
dependencies:
koin: ^[version]
koin_flutter: ^[version]
// Given some classes
class Bloc {
final Repository service;
Bloc(this.service);
get state => "Hello";
}
class Repository {}
// just declare your providers(definitions)
final myModule = Module()
// Declare a single provider(definition) for Bloc class
..single((s) => Bloc(s.get()))
// Declare a single provider(definition) for Repository class
..single((s) => Repository());
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.module(homeModule);
});
runApp(MyApp());
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
// Get a dependency
final bloc = get<Bloc>();
return Container(
child: Text("${bloc.state()}"),
);
}
}
dependencies:
koin: ^[version]
# Koin for Unit tests
dev_dependencies:
koin_test: ^[version]
dependencies:
koin: ^[version]
koin_flutter: ^[version]
# Koin for Unit tests
dev_dependencies:
koin_test: ^[version]
dependencies:
koin: ^[version]
koin_flutter: ^[version]
koin_bloc: ^[version]
# Koin for Unit tests
dev_dependencies:
koin_test: ^[version]
An simple example in Flutter. Code: Repository
A more elaborate example using Bloc library as a state management. Code: Repository
A application to demonstrate the Koin in a real world application.
- Log in
- Sign up
- Loggout
- Password reset
Code: Repository
Koin DevTools allows you to inspect the internal state of the objects created by the providers(definitions).
Just insert the KoinDevTools Widget somewhere in your application or use showDevTools.
class Page extends StatefulWidget {
@override
_PageState createState() => _PageState();
}
class _PageState extends State<Page> {
@override
Widget build(BuildContext context) {
return Scaffold(
/// Just insert the KoinDevTools
endDrawer: KoinDevTools(),
body: IconButton(icon: Text('Shod DevTools'), onPressed: () {
// Or use this
showDevTools(context);
},),
);
}
}
- Post your question on Stackoverflow - #koindart tag
Found a bug on a specific feature? Open an issue on Github issues
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
- Arnaud Giuliani and all contributors to the original Koin version written in Kotlin.
- Kt.dart port by Pascal Welsch