--- name: flutter-di description: Use when registering or resolving a dependency with flutter_injections - adding a registration to the core injections, assembling a feature FlutterModule, choosing between singleton/lazySingleton/factory, or calling FlutterInjections.get. Triggers on a runtime "dependency not registered" error. --- You are configuring dependency injection for a Flutter feature using `flutter_injections`. ## DI Architecture Overview ``` FlutterInjectionsWidget (root, in AppWidget) └── CoreInjections.core() ← global singletons (Dio, repos, session) ├── auth() ├── services() ├── eventBus() └── ... FlutterModule (per-module, lazy) └── HomeModule.injections ← scoped to module lifetime ├── HomeController └── HomeConsumer ``` ## Registration Lifetimes | Type | Usage | Pattern | |------|-------|---------| | `singleton` | One instance for app lifetime | Dio, Session, Broker | | `lazySingleton` | Created on first use, then kept | Controllers, Repositories | | `factory` | New instance every `get()` call | Usecases, one-time actions | ## Step 1: Register in CoreInjections File: `core/lib/injections.dart` Add a new static method for the domain group: ```dart class CoreInjections { static List> core() => [ ...auth(), ...services(), ...eventBus(), ...(), // ← ADD THIS ]; // ← ADD THIS METHOD static List> () => [ Inject<Datasource>( (i) => Datasource(i.find()), ), Inject<Repository>( (i) => RepositoryImpl(i.find<Datasource>()), ), ]; } ``` ## Step 2: Register controller in the Module File: `modules//_module.dart` Extend **`FlutterModule`** and override its two getters, `injections` and `child`. `FlutterModule` is a `StatelessWidget` that wraps its `child` in a `FlutterInjectionsWidget` for you — don't build that wrapper by hand. ```dart class Module extends FlutterModule { const Module({super.key}); @override List> get injections => [ Inject<Controller>.lazySingleton( (i) => Controller( GetUsecase(i.find<Repository>()), ), ), ]; @override Widget get child => BlocProvider( create: (_) => FlutterInjections.get<Controller>(), child: const Screen(), ); } ``` Both getters are `@override` — they are abstract on `FlutterModule`, so omitting either is a compile error rather than a silent no-op. ## Step 3: Resolve dependencies **In widgets/screens (via BlocProvider):** ```dart context.read<Controller>() ``` **Programmatic resolution (outside widget tree):** ```dart final controller = FlutterInjections.get<Controller>(); ``` **Inside an Inject factory (chain resolution):** ```dart Inject.lazySingleton( (i) => OrderController( getOrders: i.find(), broker: i.find(), ), ), ``` ## Patterns to Follow ### Usecase as factory (stateless, safe to create per-use) ```dart InjectUsecase>( (i) => GetUsecase(i.find<Repository>()), ), ``` ### Controller as lazySingleton (stateful, one per scope) ```dart Inject<Controller>.lazySingleton( (i) => Controller(i.findUsecase>()), ), ``` ### Repository as singleton (shared data layer) ```dart Inject<Repository>( (i) => RepositoryImpl(i.find<Datasource>()), ), ``` ## Event Bus DI When a module needs to publish/subscribe events via Broker: ```dart Inject( (i) => PaymentConsumer(broker: i.find()), ), ``` ## Common Mistakes to Avoid 1. **Do NOT** register controllers in `CoreInjections` — they belong in the module scope 2. **Do NOT** register usecases as singletons — they are stateless and should be factories 3. **Always** use `i.find()` inside Inject factory, never `FlutterInjections.get()` (which bypasses scope) 4. **Order matters** — list dependencies before dependents in the injections list ## Instructions 1. Read `core/lib/injections.dart` to understand existing registrations 2. Find the right scope: global (CoreInjections) vs module-scoped (Module.injections) 3. Apply the correct lifetime: singleton/lazySingleton/factory 4. Register datasource → repository in Core; usecase + controller in Module 5. Check for circular dependencies before registering