Skip to content
All writing
Architecture1 Sept 20263 min read

How I structure production Flutter applications

The folder layout, the boundaries, and the rules I keep even when a deadline says not to.

Every Flutter project starts the same way, with one lib/ folder and good intentions. The difference between a codebase that is still pleasant at month eighteen and one that is not usually comes down to a handful of decisions made early.

Here is what I do.

Feature-first, always

code
lib/
  features/
    auth/
    orders/
    profile/
  core/
    network/
    theme/
    errors/
    di/
  main.dart

Not screens/, models/, services/. Layer-first folders look organised in a screenshot and are miserable in practice: adding one field to an order means opening four directories. Feature-first means everything about orders lives in one place, and deleting a feature is deleting a folder.

Inside a feature:

code
orders/
  data/          api client, models, repository implementation
  domain/        entities, repository interface, use cases with real logic
  presentation/  bloc, pages, widgets

Widgets hold no business rules

If a widget knows the discount is 10% over 5,000 taka, that rule now lives in a place nobody will think to look when it changes. It also cannot be tested without building a widget tree.

The rule I hold to: a widget can decide how something looks. It cannot decide what is true.

One place that talks to the network

Every feature goes through a repository. No widget imports Dio, ever. This is the single highest-value boundary in a Flutter app, because backend responses change more often than anything else in the project, and without it every response change is a UI change.

dart
abstract class OrderRepository {
  Future<Result<List<Order>>> getOrders({String? cursor});
}

The Result type matters as much as the interface. Throwing across layers means every caller has to remember what might come out. Returning success or failure explicitly means the compiler reminds them.

Errors get converted at the boundary

DioException should not exist above the data layer. Convert it once:

dart
Failure toFailure(Object error) => switch (error) {
  DioException(type: DioExceptionType.connectionTimeout) => const NetworkFailure(),
  DioException(response: final r?) when r.statusCode == 401 => const AuthFailure(),
  DioException(response: final r?) when r.statusCode! >= 500 => const ServerFailure(),
  _ => const UnknownFailure(),
};

Now the BLoC handles four cases it understands instead of a library type it should not know about, and the UI can show a real message instead of "Exception".

Dependency injection, kept boring

One get_it registration file per feature, wired up at start. That is it. I do not build a clever service locator abstraction on top. The goal is that a test can swap a repository for a fake in one line. Anything past that is not buying anything.

Environment config that is not a constant file

dart
class Env {
  static const baseUrl = String.fromEnvironment('BASE_URL');
}

Passed with --dart-define. Keys and URLs do not sit in Git, and switching between staging and production is a build flag, not a commented-out line someone will ship by accident.

The rules I keep under deadline pressure

When a release is close, some things get skipped. These do not:

  • No network calls in widgets.
  • No business rules in widgets.
  • New feature goes in a feature folder, not in core/.

They cost minutes now. Skipping them costs days later, and the days always land on someone else.

What this does not solve

Structure will not save a project from unclear requirements or a backend that returns a different shape for the same endpoint depending on the day. It just makes sure the mess is in one place instead of spread across forty widgets.