Skip to content
Back to skills

Flutter Dependency Injection

ASecurity

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/injec...

  • 584 stars
  • 0 votes
  • 0 copies
  • 4 views
  • Added September 5, 2026
developmentgoshell

Security analysis

A100/100

Pro scans all 12 files and shows the line behind each finding

Scanned September 5, 2026

npx -y skills add HoangNguyen0403/agent-skills-standard --skill flutter-dependency-injection --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Flutter Dependency Injection?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Flutter Dependency Injection
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/hoangnguyen0403-flutter-dependency-injection-89c83f3b/badge)](https://www.skillsdirectory.com/skills/hoangnguyen0403-flutter-dependency-injection-89c83f3b)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
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.md2.1 KB
  • eval-1.with-skill.md2.4 KB
  • eval-2.baseline.md2 KB
  • eval-2.with-skill.md1.8 KB
  • eval-3.baseline.md2.5 KB
  • eval-3.with-skill.md2.4 KB
  • trigger-1.md147 B
  • trigger-2.md138 B
  • trigger-3.md161 B
  • trigger-4.md154 B
  • trigger-5.md138 B

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.