Learn/Architecture Patterns/Clean Architecture

Clean Architecture

Structure your Flutter project with Clean Architecture layers

8 min~5 min read+40 FX

You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.

Table of Contents

  1. 1. Boundary Enforcement Strategy
  2. 2. This Lesson In Simple Words
  3. 3. Your level focus
  4. 4. One practical habit
  5. 5. Deep Understanding Guide
  6. 6. Why this lesson matters
  7. 7. Study flow for better retention
  8. 8. Quick Glossary
  9. 9. Architecture Lens

Having trouble? AI is here to help

Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.

Use the AI button at the bottom of the screen

Clean Architecture

Clean Architecture is a software design approach that organizes code into layers with strict dependency rules. The inner layers contain your core business logic and know nothing about the outer layers. This makes your code framework-independent, meaning you could swap Flutter for another framework and your business logic would remain untouched. In Flutter projects, Clean Architecture typically uses three layers. The Presentation layer contains widgets, screens, and state management classes. The Domain layer holds your business entities, use case classes, and abstract repository interfaces. The Data layer implements those interfaces and handles API calls, database operations, and local caching.

dart
1// ── Folder Structure ──
2// lib/
3// features/
4// auth/
5// data/
6// datasources/
7// auth_remote_datasource.dart
8// models/
9// user_model.dart
10// repositories/
11// auth_repository_impl.dart
12// domain/
13// entities/
14// user.dart
15// repositories/
16// auth_repository.dart (abstract)
17// usecases/
18// login_usecase.dart
19// presentation/
20// pages/
21// login_page.dart
22// providers/
23// auth_provider.dart
24
25// ── Domain Layer: Entity ──
26class User {
27 final String id;
28 final String name;
29 final String email;
30 const User({required this.id, required this.name, required this.email});
31}
32
33// ── Domain Layer: Repository (abstract) ──
34abstract class AuthRepository {
35 Future<User> login(String email, String password);
36 Future<void> logout();
37}
38
39// ── Domain Layer: Use Case ──
40class LoginUseCase {
41 final AuthRepository repository;
42 LoginUseCase(this.repository);
43
44 Future<User> call(String email, String password) {
45 return repository.login(email, password);
46 }
47}
48
49// ── Data Layer: Repository Implementation ──
50class AuthRepositoryImpl implements AuthRepository {
51 final AuthRemoteDataSource remoteDataSource;
52 AuthRepositoryImpl(this.remoteDataSource);
53
54 @override
55 Future<User> login(String email, String password) async {
56 final model = await remoteDataSource.login(email, password);
57 return model.toEntity(); // Convert model to domain entity
58 }
59
60 @override
61 Future<void> logout() => remoteDataSource.logout();
62}

The Dependency Rule

💡 The Dependency Rule

Dependencies point INWARD. Presentation → Domain ← Data. The domain layer NEVER imports from data or presentation layers. This makes business logic testable and framework-independent.

Boundary Enforcement Strategy

Clean Architecture fails when boundaries are documented but not enforced in code review. Use these guardrails:

  • Domain entities must remain framework-agnostic and immutable where possible.
  • Use cases should orchestrate business rules, not UI formatting or storage details.
  • Data layer converts DTO and model objects into domain entities through mappers.
  • Presentation layer depends on use cases, never directly on API or database services.

This discipline keeps refactoring safe when data sources or UI frameworks change.

Clean Architecture Review Checklist

  • No domain file imports from Flutter widgets or third-party data clients.
  • Repository interfaces are in domain and implementations are in data.
  • Use cases are small and focused on one business action.
  • Mapping logic is explicit, tested, and not duplicated across layers.
  • Presentation errors are translated from domain failures into user-safe messages.

This Lesson In Simple Words

Structure your Flutter project with Clean Architecture layers. This lesson adds one more building block for creating stable and user-friendly Flutter apps.

Your level focus

Think in architecture, reliability, and team-level maintainability.

One practical habit

After reading, rewrite the concept in your own words and apply it in a tiny runnable sample.

Deep Understanding Guide

Why this lesson matters

Each lesson becomes useful when you connect concept, implementation, and real-user outcome together.

Study flow for better retention

  1. Separate UI, state, and data boundaries before writing implementation details.
  2. Use a simple cycle: understand, implement, test, and explain.
  3. If another developer can read your flow quickly, your design choices are clear.

Common Mistakes To Avoid

  • Moving forward without checking whether the current concept is truly clear.
  • Copying code exactly without experimenting with small changes.
  • Skipping edge cases and only testing happy-path behavior.
  • Assuming one pattern works for every scenario without trade-off analysis.

Practice Sprint (15-25 Minutes)

  • Before running sample code, predict output and behavior in one sentence.
  • Build one tiny example from memory after finishing this lesson.
  • Write one paragraph about where this concept fits in a real app.
  • Create one failure scenario and verify your app handles it safely.
  • Complete the required exercises without using hints on the first attempt.
  • Answer this question out loud before checking quiz options: "In Clean Architecture, which layer contains business logic?".

Quick Glossary

  • widget: A reusable UI building block in Flutter.
  • state: Data that can change and update what the user sees.
  • repository: A layer that hides data source details from the UI layer.

Code Comprehension Drill

  • State the purpose of the code block in one line before reading details.
  • Mark inputs, state changes, and final output.
  • Change one small value and predict what should happen before running.
  • Run again and compare your prediction with actual behavior.

Architecture Lens

Connect this lesson to separation of concerns so your codebase stays maintainable as it grows.

As your app grows, keep UI, state, and data responsibilities separate so each part stays testable and easier to modify.

Lesson Completion Checklist

  • I can explain "Clean Architecture" in simple words without reading notes.
  • I can describe one real app situation where this concept is required.
  • I know at least one mistake to avoid: Moving forward without checking whether the current concept is truly clear.
  • I can modify one code sample and correctly predict the behavior change.
  • I can finish required exercises with understanding, not trial-and-error guesses.

Frequently Asked Questions

In Clean Architecture, which layer contains business logic?

The Domain layer contains entities, use cases, and repository interfaces: the core business logic.

What is the Dependency Rule in Clean Architecture?

The Dependency Rule says inner layers cannot know about outer layers. Domain is the innermost layer.

Where is the concrete repository implementation located?

The abstract repository interface is in Domain. The implementation is in Data layer, which handles actual API/database calls.

Suggest Improvement

Something seems incorrect, unclear, or missing? Let us know.

About The Author

FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.

Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.

Sign in to continue with exercises and quiz for this lesson.

Sign In to Continue