What is wiretap?#
wiretap is a extremely lightweight, type-safe dependency injection library for Dart. It uses declaritive typed keys to define injectable values and providers to configure how values are resolved. You compose a
WiretapContext to supply and override dependencies.
Highlights#
- Zero dependencies*, minimal runtime cost
- Strong typing with generics
- Declarative defaults via tokens
- Protection against circular dependencies
- Predictable overrides and ordering
- Works in pure Dart and Flutter
*While wiretap would like to use no dependencies, the official meta package is still required to provide analyzer feedback. This is the only dependency in use though.
Getting Started#
Installation#
Add the package to your pubspec.yaml by running dart pub add wiretap or flutter pub add wiretap.
First steps#
Define tokens, create a context, and resolve values.
import 'package:wiretap/wiretap.dart';
final configToken = createValueToken(Config(apiUrl: 'https://api.example.com'));
final httpClientToken = createToken((_) => HttpClient());
final apiServiceToken = createToken((inject) =>
ApiService(inject(httpClientToken), inject(configToken))
);
final context = WiretapContext();
final apiService = context.inject(apiServiceToken);
Overriding values#
Override dependencies per environment or test.
final testContext = WiretapContext(providers: [
httpClientToken.provideOverride((_) => MockHttpClient()),
configToken.provideOverride((_) => TestConfig()),
]);
final testApiService = testContext.inject(apiServiceToken);
Collections#
Aggregate multiple values of the same type. This can be used to provide implementations of the same interface:
final pluginToken = provideCollection<Plugin>();
final context = WiretapContext(providers: [
pluginToken.provideAdditional((_) => [AuthPlugin(), LoggingPlugin()]),
pluginToken.provideAdditional((_) => [MetricsPlugin()]),
]);
final plugins = context.inject(pluginToken);
Next: Learn the building blocks in Core Concepts.