Skip to main content

cobalt 1.2.0 copy "cobalt: ^1.2.0" to clipboard
cobalt: ^1.2.0 copied to clipboard

Dependency injection for Dart and Flutter. Scopes close everything they built when they end, async services start in dependency order, and tests can override any dependency.

example/example.md

cobalt example #

The runtime, with no code generation — Manual Mode. The generator writes exactly this, using only what is exported here.

import 'package:cobalt/cobalt.dart';

class Database {
  var isOpen = false;
}

final class DatabaseFactory implements CobaltFactory<Database> {
  const DatabaseFactory();

  @override
  Database create(CobaltResolver resolver) => Database();
}

class AppScope implements CobaltScopeBuilder {
  const AppScope();

  @override
  void build(CobaltScope scope) =>
      scope.registerLazySingleton<Database>(const DatabaseFactory());
}

Future<void> main() async {
  final app = await CobaltApplication.start(
    root: const AppScope(),
    rootName: 'app',
  );

  print(app.get<Database>());

  // A child scope: everything built inside dies with it, in reverse order of
  // creation. This is what makes sign-out a single call.
  final session = app.push('session:42');
  await session.init();
  await session.dispose();

  await app.dispose();
}

More: examples/manual_mode.

2
likes
0
points
1.61k
downloads

Publisher

unverified uploader

Weekly Downloads

Dependency injection for Dart and Flutter. Scopes close everything they built when they end, async services start in dependency order, and tests can override any dependency.

Repository (GitHub)
View/report issues

Topics

#dependency-injection #di #service-locator #scopes

License

unknown (license)

Dependencies

cobalt_annotations, collection, meta

More

Packages that depend on cobalt