Flutter Dependency Injection
ASecurityRegister it in three layers, following the skill’s DI rules: 1. Put third-party dependencies like `Dio` and Hive objects behind an `@module` 2. Register the repository as a `@LazySingleton`, because repositories should usually be shared and initialized on demand 3. Inject the repository through its abstraction, not the concrete class Example: ```dart import 'package:dio/dio.dart'; import 'package:hive/hive.dart'; import 'package:injectable/injectable.dart'; abstract class IOrderRepository { F...
- 584 stars
- 0 votes
- 0 copies
- 2 views
- Added September 5, 2026
Works with
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-agent-skills-standard)Register it in three layers, following the skill’s DI rules:
1. Put third-party dependencies like `Dio` and Hive objects behind an `@module`
2. Register the repository as a `@LazySingleton`, because repositories should usually be shared and initialized on demand
3. Inject the repository through its abstraction, not the concrete class
Example:
```dart
import 'package:dio/dio.dart';
import 'package:hive/hive.dart';
import 'package:injectable/injectable.dart';
abstract class IOrderRepository {
Future<void> submitOrder();
}
class OrderEntity {}
@module
abstract class DataModule {
@lazySingleton
Dio get dio => Dio(
BaseOptions(baseUrl: 'https://api.example.com'),
);
@preResolve
Future<Box<OrderEntity>> get orderBox async {
return await Hive.openBox<OrderEntity>('orders');
}
}
@LazySingleton(as: IOrderRepository)
class OrderRepository implements IOrderRepository {
final Dio _dio;
final Box<OrderEntity> _orderBox;
OrderRepository(this._dio, this._orderBox);
@override
Future<void> submitOrder() async {
// use _dio for remote calls
// use _orderBox for local persistence/cache
}
}
```
Why this matches the skill:
- `@module` is the right place for external dependencies you cannot annotate directly
- `@LazySingleton` is the default choice for repositories and data sources
- `as: IOrderRepository` keeps consumers dependent on the interface instead of the implementation
- constructor injection keeps the dependency graph explicit and testable
If you want a cleaner boundary around Hive, an even better version is to wrap the box in a local data source and inject that instead of injecting `Box<OrderEntity>` directly:
```dart
abstract class IOrderLocalDataSource {}
@LazySingleton(as: IOrderLocalDataSource)
class HiveOrderLocalDataSource implements IOrderLocalDataSource {
final Box<OrderEntity> box;
HiveOrderLocalDataSource(this.box);
}
@LazySingleton(as: IOrderRepository)
class OrderRepository implements IOrderRepository {
final Dio _dio;
final IOrderLocalDataSource _local;
OrderRepository(this._dio, this._local);
}
```
That version is usually easier to mock and keeps Hive-specific details out of the repository contract.
After adding the annotations, regenerate the injectable code so `get_it` picks up the new registrations. The key idea is: module for `Dio` and Hive, `@LazySingleton` for the repository, and inject the interface everywhere else.
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.