cobalt
1.9.2, released 4 weeks ago by an unverified publisher
Dependency injection for Dart and Flutter with scopes, async ordering, and tests.
WASM-ready
What it does
Registers services in scopes, resolves dependencies, and manages lifecycle teardown deterministically. Supports async startup graphs and optional code generation via build_runner.
AI-written summary of version 1.9.2, based on the package's README and metadata. Report an inaccuracy
Use it when
- You need hierarchical dependency scopes that clean up resources on dispose
- You have complex async initialization orders between services
- You want a pure Dart DI solution that works across Flutter, CLI, and server
Skip it when
- You prefer global service locators without explicit scoping
- You want a complete state management framework rather than just DI
Strengths
- Deterministic teardown in reverse creation order
- Topological sorting for async dependency startup
- Optional code generation allowing manual and generated factories to mix
Weaknesses
- Scopes dropped without dispose leak resources by design
- Requires explicit teardown handling for third-party objects that do not implement Disposable
Setup
- Kind
- Pure Dart
- Effort
- Low
- Code generation
- Yes (build_runner)
- Direct dependencies
- 3
Add cobalt to pubspec.yaml. Optional use of build_runner for code generation.
Future<void> main() async {
final app = CobaltScope.root(name: 'app')
..registerLazySingleton<Clock>(const ClockFactory())
..registerLazySingleton<Greeter>(const GreeterFactory());
await app.init();
print(app.get<Greeter>().greet('Cobalt'));
await app.dispose();
}Alternatives
Packages that do the same job and could replace cobalt.
- get_it
If you prefer a simpler global service locator without explicit scope hierarchies.
89Compare - injectable
If you want annotation-driven dependency injection with get_it as the underlying locator.
78Compare