Back to Articles

Advanced

Layered Architecture in Flutter with Clean Architecture

· 10 min read

What Does Clean Architecture Solve?

In a small app, it's tempting to fire off an HTTP request or run a Firestore query right inside a widget's build method; it gets results fast. But as the project grows, this approach causes two problems: it becomes hard to test business logic with widget tests (every test needs a real network connection or a mocked widget tree), and switching data sources (say, moving from a REST API to GraphQL) means touching UI code everywhere. Clean Architecture reverses this dependency by splitting responsibilities into three layers.

Three Layers: Presentation, Domain, Data

• Presentation: Widgets, state management (Provider/Riverpod/Bloc) and user interaction. It only calls UseCases in the Domain layer and doesn't know whether the data source is Firestore or a REST API. • Domain: Pure Dart code holding the app's business rules. Entities (data models), UseCases (classes each representing a single workflow), and Repository interfaces (abstract classes) live here. It depends on no Flutter widget or external package. • Data: Concrete implementations of the Repository interfaces, API clients, local database access, and mappers that turn these into Domain entities.

The Dependency Rule

The core rule is this: outer layers may depend on an inner layer, but the inner layer may never depend on an outer one. Presentation depends on Domain; Data also depends on Domain (it implements Domain's Repository interface). But Domain imports neither Presentation nor Data. This means the Domain layer stays unchanged even if you move from Firestore to sqflite, or from Provider to Bloc.

Example: Product List

In the Domain layer, you first define the Entity and the Repository interface:

// domain/entities/urun.dart
class Urun {
  const Urun({required this.id, required this.ad, required this.fiyat});

  final String id;
  final String ad;
  final double fiyat;
}

// domain/repositories/urun_repository.dart
abstract class UrunRepository {
  Future<List<Urun>> urunleriGetir();
}

// domain/usecases/urunleri_getir_usecase.dart
class UrunleriGetirUseCase {
  UrunleriGetirUseCase(this._repository);

  final UrunRepository _repository;

  Future<List<Urun>> call() => _repository.urunleriGetir();
}

The Data layer provides the concrete implementation; Firestore-specific code lives only in this file:

// data/repositories/urun_repository_impl.dart
class UrunRepositoryImpl implements UrunRepository {
  UrunRepositoryImpl(this._firestore);

  final FirebaseFirestore _firestore;

  @override
  Future<List<Urun>> urunleriGetir() async {
    final snapshot = await _firestore.collection('urunler').get();
    return snapshot.docs
        .map((doc) => Urun(
              id: doc.id,
              ad: doc['ad'] as String,
              fiyat: (doc['fiyat'] as num).toDouble(),
            ))
        .toList();
  }
}

The widget in the Presentation layer knows neither Firestore nor UrunRepositoryImpl; it only calls the UseCase. This means that when you write a widget test, you need a fake implementation of the UseCase, not a real Firestore connection.

class UrunListesiSayfasi extends StatelessWidget {
  const UrunListesiSayfasi({super.key, required this.useCase});

  final UrunleriGetirUseCase useCase;

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<List<Urun>>(
      future: useCase(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) return const CircularProgressIndicator();
        return ListView(
          children: [for (final urun in snapshot.data!) Text(urun.ad)],
        );
      },
    );
  }
}

Things to Watch Out For

1. Not Every Small Project Needs It

Adding three layers, UseCase classes and mappers to a one-screen demo app adds unnecessary complexity (over-engineering). Clean Architecture proves its value in mid-to-large projects worked on by multiple developers, built for the long term, where test coverage matters.

2. Keep UseCases Genuinely Single-Responsibility

A UseCase should have a single call() method and a single responsibility (for example, UrunleriGetirUseCase only fetches products, it doesn't also handle adding to a cart). Cramming multiple jobs into one UseCase erases the testability advantage layering gives you.

Conclusion

The essence of Clean Architecture is simple: keep your business logic (Domain) independent from both the UI (Presentation) and the data source (Data), and make sure the dependency always flows inward. This lets you test UseCases without mocking the UI, and swap the data source without touching Domain or Presentation. But setting this structure up has a real cost — if your project's scale and lifespan don't justify it, prefer a simpler structure.