Skip to content

Quick Start

This page walks through the minimum you need to wire an app with CherryPick. It assumes you have already installed the cherrypick package.

A module groups the bindings that tell CherryPick how to build each dependency. Override builder and call bind<T>() for every type you want to provide:

import 'package:cherrypick/cherrypick.dart';
class AppModule extends Module {
@override
void builder(Scope currentScope) {
// A ready-made instance.
bind<ApiClient>().toInstance(ApiClientMock());
// A factory, called on every resolve.
bind<String>().toProvide(() => "Hello, CherryPick!");
}
}

The root scope is the top of the dependency tree. Install your modules into it once, at startup:

final rootScope = CherryPick.openRootScope();
rootScope.installModules([AppModule()]);

Ask the scope for a type. Use resolveAsync for async bindings and tryResolve when a missing binding is a normal case rather than an error — see the Dependency Resolution API.

final greeting = rootScope.resolve<String>();
print(greeting); // prints: Hello, CherryPick!

When the app (or test) is done, close the scope. This disposes every Disposable the scope owns. Always await it — even if nothing is disposable today:

await CherryPick.closeRootScope();

Add cherrypick_flutter and wrap your app in CherryPickProvider. Widgets below it can reach the scopes through CherryPickProvider.of(context):

import 'package:cherrypick/cherrypick.dart';
import 'package:cherrypick_flutter/cherrypick_flutter.dart';
import 'package:flutter/material.dart';
void main() {
CherryPick.openRootScope().installModules([AppModule()]);
runApp(const CherryPickProvider(child: MyApp()));
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
final scope = CherryPickProvider.of(context).openRootScope();
return MaterialApp(home: Text(scope.resolve<String>()));
}
}

CherryPickProvider.of(context).openSubScope(scopeName: 'feature') opens a subscope for a feature or a screen.