الانتقال إلى المحتوى
تطبيقات الموبايلApp Architecture· 7 دقائق للقراءة

Use Cases Are Optional: What Flutter's Official Architecture Guide Actually Recommends

Flutter's guide strongly recommends two layers, views, view models and repositories. The domain layer is conditional: add a use case when the logic earns it.

ReadIn

مقال كل يوم يفرق معاك

22 نوفمبر 2026
  • كاتب موثّق
  • Flutter 3.x
محتويات المقال

A three-person team adds a dark mode switch to its Flutter app. The pull request touches nine files: an abstract repository and its implementation, a local data source, GetThemeModeUseCase and SetThemeModeUseCase, an entity, a model, a mapper and a view model. A reviewer asks why a switch needs nine files. The author answers that this is clean Flutter architecture, the way the official guide says to do it. Then the reviewer opens the guide and finds the domain layer under a heading that starts with "Optional".

The common misconception

The belief: good Flutter architecture means three layers, presentation, domain and data, for every feature, and every action the UI takes goes through its own use-case class, no matter how simple.

It's a reasonable belief. Clean Architecture templates for Flutter look exactly like this, and starter kits generate the folders for you. A fixed shape feels safe: every feature looks the same, and nobody argues about where code goes. Use cases are easy to unit test. And the official guide does describe a domain layer, with use cases, and its sample app has a few. The half-truth: use cases are a real tool that the guide recommends, but only under conditions it spells out.

What actually happens

The guide's core is two layers, not three

The guide calls separation of concerns "the most important principle" and says your app "should split into two broad layers, the UI layer and the Data layer." It recommends four components: views, view models, repositories and services. In terms, views and view models are the UI layer, and repositories and services are the model.

Its description of a feature is just as specific: one view, one view model, "one or more repositories as the sources of truth for your application data, and zero or more services". No use case appears in that list. The domain layer shows up first in a note: "Apps with complex logic might also have a logic layer".

"Conditional" is the official rating

The recommendations page rates every practice: "Strongly recommend", "Recommend" or "Conditional". Clearly defined UI and data layers, the repository pattern, MVVM and abstract repository classes are all strongly recommended. "Use a domain layer" is conditional, with a blunt explanation: "In very large apps, use-cases are useful, but in most apps they add unnecessary overhead."

The guide gives three conditions for moving logic out of a view model into a . The logic merges data from several repositories, it's "exceedingly complex", or it's reused by different view models. If none applies, the guide's answer is direct: "This layer is optional because not all applications or features within an application have these requirements."

Add them when needed, not up front

The guide also asks whether view models may still read repositories directly, or must always go through use cases. If you force a use case "each time a view model needs data, even if the logic in the use-case is simple", your code will be modular and testable, "but it also adds a significant amount of unnecessary overhead." Its advice: "add use-cases only when needed." The guide's own example app practices this: it has use cases for some features and view models that talk to repositories directly.

Android's architecture guidance, which Flutter's closely resembles, says the same: the domain layer is "optional", and "A good approach is to add use cases only when required." It also warns that its "domain layer" differs from the one in Clean Architecture, so the two vocabularies shouldn't be mixed up. A pass-through use case is a small case of : an abstraction built for a need that hasn't arrived.

Cutting the pass-through, keeping the real one

Dart settings_view_model.dart
// One use case per action, "because Clean Architecture"class GetThemeModeUseCase {  GetThemeModeUseCase(this._settings);  final SettingsRepository _settings;  // A class, a constructor parameter and a test fake. No logic.  Future<ThemeMode> call() => _settings.themeMode();}class SettingsViewModel extends ChangeNotifier {  SettingsViewModel(this._getThemeMode);  final GetThemeModeUseCase _getThemeMode;  ThemeMode mode = ThemeMode.system;  Future<void> load() async {    mode = await _getThemeMode();    notifyListeners();  }}

The settings view model is just as testable without the use case: give it a fake SettingsRepository, which the guide recommends anyway. BookTripUseCase is different. It coordinates two repositories, and both the booking screen and a "book again" button need it, so it meets two of the guide's three conditions. Using Bloc or Riverpod instead of ChangeNotifier doesn't change the reasoning; the case study says the same app could have been written with them.

PracticeFlutter's rating
Separate UI and data layersStrongly recommend
Repositories and services in the data layerStrongly recommend
Views and view models (MVVM)Strongly recommend
Abstract repository classesStrongly recommend
A domain layer with use casesConditional
Separate API models and domain modelsConditional, for large apps

The same idea runs through ReadIn's articles on coupling and cohesion and on Clean Architecture as a dependency rule: layers and classes are tools for managing change, not a score to maximize.

How to apply it in your project

  1. Audit your use casesFor each one, ask: does it merge repositories, hold complex logic, or serve more than one view model? If the answer is no to all three, inline it into the view model.
  2. Keep the strongly recommended partsViews without logic, view models per feature, repositories as the source of truth, services per data source, and abstract repositories with fakes for tests.
  3. Allow direct repository accessLet view models call repositories unless a use case adds something. Both guides suggest restricting access only if nearly all data already flows through use cases.
  4. Extract on evidenceWhen duplication or cross-repository logic appears, create the use case then, and name it after its action.
  5. Write the rule downPut "use cases when needed" in your architecture notes, so reviews stop asking for a use case per feature.

Key takeaways

  • Flutter's guide strongly recommends two layers: UI (views, view models) and data (repositories, services).
  • The domain layer is officially "Conditional"; the guide says that in most apps use cases "add unnecessary overhead".
  • Add a use case when logic merges repositories, is very complex, or is reused by several view models.
  • View models may call repositories directly; a pass-through use case adds files, not value.
  • Skipping use cases never means putting logic in widgets.

📚 المصادر

  1. Guide to app architecture — Flutter docs
  2. Architecture recommendations and resources — Flutter docs
  3. Architecture case study — Flutter docs
  4. Domain layer — Android Developers

التعليقات (0)