Flutter Layer Based Clean Architecture
ASecurityRemove the HTTP call from the widget. Define an order domain contract, implement the request and DTO mapping in infrastructure, and have a Cubit/BLoC coordinate the use case. The widget should only trigger the event and render state. ```dart // lib/domain/repositories/order_repository.dart abstract interface class OrderRepository { Future<Either<Failure, List<Order>>> fetchOrders(); } ``` ```dart // lib/infrastructure/datasources/order_remote_data_source.dart class DioOrderRemoteDataSource im...
- 584 stars
- 0 votes
- 0 copies
- 8 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-layer-based-clean-architecture --agent claude-codeAre you the author of Flutter Layer Based Clean Architecture?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/hoangnguyen0403-flutter-layer-based-clean-architecture-bb67cb7d)Remove the HTTP call from the widget. Define an order domain contract, implement the request and DTO mapping in infrastructure, and have a Cubit/BLoC coordinate the use case. The widget should only trigger the event and render state.
```dart
// lib/domain/repositories/order_repository.dart
abstract interface class OrderRepository {
Future<Either<Failure, List<Order>>> fetchOrders();
}
```
```dart
// lib/infrastructure/datasources/order_remote_data_source.dart
class DioOrderRemoteDataSource implements OrderRemoteDataSource {
DioOrderRemoteDataSource(this._dio);
final Dio _dio;
@override
Future<List<OrderDto>> fetchOrders() async {
final response = await _dio.get('/orders');
return (response.data as List<dynamic>)
.map((json) => OrderDto.fromJson(json as Map<String, dynamic>))
.toList();
}
}
// lib/infrastructure/repositories/order_repository_impl.dart
class OrderRepositoryImpl implements OrderRepository {
OrderRepositoryImpl(this._remote);
final OrderRemoteDataSource _remote;
@override
Future<Either<Failure, List<Order>>> fetchOrders() async {
try {
final dtos = await _remote.fetchOrders();
return right(dtos.map((dto) => dto.toDomain()).toList());
} on DioException catch (error) {
return left(Failure.network(error.message ?? 'Unable to load orders'));
}
}
}
```
`OrderDto.toDomain()` maps wire-format fields to the immutable domain `Order`, so neither the generated DTO nor raw JSON crosses the infrastructure boundary.
```dart
// lib/application/orders/orders_cubit.dart
class OrdersCubit extends Cubit<OrdersState> {
OrdersCubit(this._repository) : super(const OrdersState.initial());
final OrderRepository _repository;
Future<void> load() async {
emit(const OrdersState.loading());
final result = await _repository.fetchOrders();
emit(result.fold(
(failure) => OrdersState.error(failure.message),
(orders) => OrdersState.loaded(orders),
));
}
}
```
The widget becomes a presentation-only consumer:
```dart
@override
void initState() {
super.initState();
context.read<OrdersCubit>().load();
}
@override
Widget build(BuildContext context) => BlocBuilder<OrdersCubit, OrdersState>(
builder: (context, state) => state.when(
initial: () => const SizedBox.shrink(),
loading: () => const Center(child: CircularProgressIndicator()),
loaded: (orders) => OrdersList(orders: orders),
error: (message) => ErrorView(message: message),
),
);
```
Register `OrderRepository` to `OrderRepositoryImpl` and the Cubit in `get_it`/`BlocProvider`. Presentation depends on the Cubit; the Cubit depends on the domain interface; only infrastructure depends on Dio.
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.