Using Annotations & Code Generation
CherryPick provides best-in-class developer ergonomics and type safety through Dart annotations and code generation. This lets you dramatically reduce boilerplate: simply annotate your classes, fields, and modules, run the code generator, and enjoy auto-wired dependency injection!
How It Works
Section titled “How It Works”- Annotate your services, providers, and fields using
cherrypick_annotations. - Generate code using
cherrypick_generatorwithbuild_runner. - Use generated modules and mixins for fully automated DI (dependency injection).
Supported Annotations
Section titled “Supported Annotations”| Annotation | Target | Description |
|---|---|---|
@injectable() |
class | Enables automatic field injection for this class (mixin will be generated) |
@inject() |
field | Field will be injected using DI (works with @injectable classes) |
@module() |
class | Declares a DI module; its methods can provide services/providers |
@provide |
method | Registers as a DI provider method (may have dependencies as parameters) |
@instance |
method/class | Registers an instance (new object on each resolution, i.e. factory) |
@singleton |
method/class | Registers as a singleton (one instance per scope) |
@named |
field/param | Use named instance (bind/resolve by name or apply to field/param) |
@scope |
field/param | Inject or resolve from a specific named scope |
@params |
param | Marks method parameter as filled by user-supplied runtime params at resolution |
You can easily combine these annotations for advanced scenarios!
Field Injection Example
Section titled “Field Injection Example”import 'package:cherrypick_annotations/cherrypick_annotations.dart';
@injectable()class ProfilePage with _\$ProfilePage { @inject() late final AuthService auth;
@inject() @scope('profile') late final ProfileManager manager;
@inject() @named('admin') late final UserService adminUserService;}- After running build_runner, the mixin
_ProfilePagewill be generated for field injection. - Call
myProfilePage.injectFields();or use the mixin’s auto-inject feature, and all dependencies will be set up for you.
Module and Provider Example
Section titled “Module and Provider Example”@module()abstract class AppModule { @singleton AuthService provideAuth(Api api) => AuthService(api);
@named('logging') @provide Future<Logger> provideLogger(@params Map<String, dynamic> args) async => ...;}- Mark class as
@module, write provider methods. - Use
@singleton,@named,@provide,@paramsto control lifecycle, key names, and parameters. - The generator will produce a class like
$AppModulewith the proper DI bindings.
Usage Steps
Section titled “Usage Steps”-
Add to your pubspec.yaml:
dependencies:cherrypick: anycherrypick_annotations: anydev_dependencies:cherrypick_generator: anybuild_runner: any -
Annotate your classes and modules as above.
-
Run code generation:
Terminal window dart run build_runner build --delete-conflicting-outputs# or in Flutter:flutter pub run build_runner build --delete-conflicting-outputs -
Register modules and use auto-injection:
final scope = CherryPick.openRootScope()..installModules([\$AppModule()]);final profile = ProfilePage();profile.injectFields(); // injects all @inject fields
Advanced: Parameters, Named Instances, and Scopes
Section titled “Advanced: Parameters, Named Instances, and Scopes”- Use
@namedfor key-based multi-implementation injection. - Use
@scopewhen dependencies live in a non-root scope. - Use
@paramsfor runtime arguments passed during resolution.
Troubleshooting & Tips
Section titled “Troubleshooting & Tips”- After modifying DI-related code, always re-run
build_runner. - Do not manually edit
.g.dartfiles—let the generator manage them. - Errors in annotation usage (e.g., using
@singletonon wrong target) are shown at build time.