Back to Journal
ArchitectureFlutterBLoCCleanCode· 9 min read

Production Flutter Architecture: BLoC, Freezed, and Dependency Inversion

H
Hridoy Khandakar•

Maintaining velocity in a growing Flutter codebase requires strict separation between UI, presentation state, domain rules, and data sources.

Layer Boundaries

We organize features using feature-first packaging:

lib/
├── core/
│   ├── network/
│   ├── storage/
│   └── error/
└── features/
    └── authentication/
        ├── data/
        │   ├── datasources/
        │   ├── models/
        │   └── repositories/
        ├── domain/
        │   ├── entities/
        │   ├── repositories/
        │   └── usecases/
        └── presentation/
            ├── bloc/
            └── screens/

Immutable States with Freezed

Handling async transitions is error-prone when states are represented by primitive booleans. Freezed gives us exhaustive pattern matching:

@freezed
class AuthState with _$AuthState {
  const factory AuthState.initial() = _Initial;
  const factory AuthState.authenticating() = _Authenticating;
  const factory AuthState.authenticated({required UserProfile user}) = _Authenticated;
  const factory AuthState.failure({required NetworkFailure failure}) = _Failure;
}

In the UI layer, BlocBuilder can safely map all cases:

state.when(
  initial: () => const LoginOptionsView(),
  authenticating: () => const FullscreenLoader(),
  authenticated: (user) => DashboardView(user: user),
  failure: (failure) => ErrorNotice(message: failure.localizedMessage),
);

This prevents unhandled null states, improves unit test coverage, and ensures UI logic remains purely declarative.

H

Written by Hridoy Khandakar

Senior Mobile App Developer with deep experience in Flutter & Dart. Building production cross-platform apps that don't compromise on speed or feel.

More about my work →