π ForceInject
π οΈ A lightweight, zero-runtime-dependency Dependency Injection (DI) container for Dart.
π Inspired by .NET Core and powered by the Force.
This is the Way.
β¨ Features
- Constructor-based dependency injection
- Support for
singleton,transient, and `scoped lifetimes - Zero dependencies β pure Dart
- No code generation, no mirrors
- Inspired by C#/.NET Core DI patterns
- Lightweight, test-friendly, and easy to integrate
βοΈ Dependencies
ForceInject is designed to have no runtime dependencies.
We use the Dart team's official meta package only for static annotations like @protected and @visibleForTesting.
This ensures:
- β No runtime or platform bloat
- β Full IDE and analyzer support
- β Maximum compatibility with Flutter, backend, and CLI
π Getting Started
1. Register your services
final services = ServiceCollection();
services.addSingleton<ILogger, ConsoleLogger>();
services.addTransient<UserService, UserService>();
ServiceProvider.registerConstructor<ConsoleLogger>(() => ConsoleLogger(), []);
ServiceProvider.registerConstructor<UserService>(
(ILogger logger) => UserService(logger),
[ILogger],
);
2. Build the provider
final provider = services.buildServiceProvider();
3. Resolve services
final userService = provider.get<UserService>();
userService.sayHello(); // [LOG]: Hello from UserService!
Need a per-screen or per-request services? Create a ServiceScope:
final scope = provider.createScope();
final viewModel = scope.get<MyViewModel>();
scope.dispose(); // clean up when done
βοΈ Lifetimes
| Type | Description |
|---|---|
singleton |
One shared instance across the application |
transient |
A new instance is created every time |
scoped |
One instance per ServiceScope β ideal for screen/request lifetimes |
π¦ Why ForceInject?
Dart is an elegant language, but backend structure is still evolving.
ForceInject gives you the power of .NETβs DI model, but in pure Dart β making your code modular, testable, and maintainable.
No magic. No mirrors. Just clean, focused architecture.
π¬ "When your services are confused, inject them you must." β Yodart
π§ͺ Examples
This package includes two complete examples you can explore:
-
π§±
minimal_dart_di
A simple Dart CLI app using ForceInject with singleton and transient services. -
π±
minimal_flutter_di
A clean Flutter app using scoped DI and aValueNotifier-based ViewModel.
To run the Flutter example:
cd example/minimal_flutter_di
flutter create .
flutter pub get
flutter run
Read the README.md inside the minimal_flutter_di project for more details.
Support me (@diegomgarcia) with Patreon
Or make a donation buying me a coffee:
You can also show support by showing on your repository that you use this lib on it with a direct link to it.
π License
MIT β Free to use, modify, or extend.
May the DI be with you. βοΈ

