Flutter Dependency Injection
ASecurityThis usually means `CartBloc` was registered with too long a lifetime. Per the skill, BLoCs should be factories, not singletons, so they start with fresh state every time the screen creates them. The fix is: 1. Register `CartBloc` with `@injectable` so injectable treats it as a factory 2. Do not use `@Singleton()` or `@LazySingleton()` for a BLoC 3. Create the bloc at the page/route level so it is disposed when the page is removed Example registration: ```dart import 'package:injectable/injec...
- 584 stars
- 0 votes
- 0 copies
- 4 views
- Added September 5, 2026
Security analysis
100/100Pro scans all 12 files and shows the line behind each finding
npx -y skills add HoangNguyen0403/agent-skills-standard --skill flutter-dependency-injection --agent claude-codeAre you the author of Flutter Dependency Injection?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/hoangnguyen0403-flutter-dependency-injection-89c83f3b)This usually means `CartBloc` was registered with too long a lifetime. Per the skill, BLoCs should be factories, not singletons, so they start with fresh state every time the screen creates them.
The fix is:
1. Register `CartBloc` with `@injectable` so injectable treats it as a factory
2. Do not use `@Singleton()` or `@LazySingleton()` for a BLoC
3. Create the bloc at the page/route level so it is disposed when the page is removed
Example registration:
```dart
import 'package:injectable/injectable.dart';
@injectable
class CartBloc extends Bloc<CartEvent, CartState> {
final ICartRepository _cartRepository;
CartBloc(this._cartRepository) : super(const CartState.initial());
}
```
Then provide it like this:
```dart
class CartPage extends StatelessWidget {
const CartPage({super.key});
@override
Widget build(BuildContext context) {
return BlocProvider<CartBloc>(
create: (_) => getIt<CartBloc>(),
child: const CartView(),
);
}
}
```
Why this works:
- `getIt<CartBloc>()` returns a new instance each time because `@injectable` is factory-scoped
- `BlocProvider(create: ...)` owns that instance and closes it when the page is popped
- when you navigate back to the page, a brand new bloc is created with the initial state
What to avoid:
- `@Singleton()` on `CartBloc`
- `@LazySingleton()` on `CartBloc`
- putting the `BlocProvider` above the navigator, shell, or tab root if you want per-screen state
- calling `getIt<CartBloc>()` directly inside widgets without a provider-owned lifecycle
Good lifetime split from the skill:
- repositories/services/data sources: `@LazySingleton`
- BLoCs: `@injectable` factory
If you still need cart data to persist, persist it in the repository or local store, not by keeping the BLoC alive forever. The BLoC should represent screen state; the repository should represent durable app data.
Files in this skill
- eval-1.baseline.md
- eval-1.with-skill.md
- eval-2.baseline.md
- eval-2.with-skill.md
- eval-3.baseline.md
- eval-3.with-skill.md
- trigger-1.md
- trigger-2.md
- trigger-3.md
- trigger-4.md
- trigger-5.md
Attribution
Related skills
View all in development →Comments
Loading comments…
Newsletter
Get the weekly list of safe new skills, and the ones we pulled.