D
ArchitectureMedium30 XP5 min read

What is Clean Architecture and how do you apply it in Flutter?

TL;DR: Clean Architecture divides the app into three layers: Domain (entities + use cases, pure Dart), Data (repositories + data sources), and Presentation (widgets + state management). Dependencies point inward — Domain knows nothing about Flutter or network.

Full Answer

The Three Layers

  • Domain: Business entities and use cases (pure Dart, no Flutter imports). Defines repository interfaces
  • Data: Implements repository interfaces. Contains API clients, local DB, mappers to/from entities
  • Presentation: Flutter widgets and state management (BLoC, Riverpod). Calls use cases, renders state

The Dependency Rule

Domain has no dependencies. Data depends on Domain (implements its interfaces). Presentation depends on Domain (calls its use cases). Data and Presentation never depend on each other.

🎯

The test of Clean Architecture: can you swap SQLite for Hive in the Data layer without touching a single use case or widget? If yes, you've done it right.

Code Examples

dartClean Architecture folder structure
Output
// Domain: testable with pure unit tests (no mocking Flutter)
// Data: swap API for GraphQL without touching domain or UI

Common Mistakes

  • Importing Flutter widgets in domain layer — breaks the dependency rule
  • Skipping the use case layer and calling the repository directly from BLoC — puts business logic in the wrong layer

Interview Tip

💡

Explain the 'screaming architecture' principle: the folder structure should scream 'this is a quiz app', not 'this uses Flutter'. Feature-first folders over layer-first.

#clean-architecture#layers#dependency-rule#solid