Building Enterprise Flutter Apps with Clean Architecture & BLoC Pattern
As cross-platform mobile engineering scales, maintaining a clean separation of concerns becomes essential. In large-scale production apps, mixing business logic with UI rendering code leads to technical debt, tight coupling, and brittle automated test suites.
As the Lead Mobile Architect and CEO of AZ MEER (SMC-PRIVATE) LIMITED, I enforce Domain-Driven Clean Architecture across all client and internal Flutter applications. In this technical publication, I present a production-tested architectural framework based on Uncle Bob's Clean Architecture, SOLID principles, BLoC state management, and dependency injection.
1. The Three Layers of Clean Architecture
- Domain Layer (Inner Core): Contains pure Dart entities, value objects, and abstract repository interfaces. Independent of Flutter or external frameworks.
- Data Layer (Middle): Implements repository interfaces, manages remote REST/GraphQL API data sources, and local database persistence (Hive/SQLite).
- Presentation Layer (Outer): Manages UI widgets, BLoC event streams, and user interactions.
// Abstract Repository Interface in Domain Layer
abstract class ProductRepository {
Future<Either<Failure, List<ProductEntity>>> getFeaturedProducts();
}
// BLoC State Management Event Handler
class ProductBloc extends Bloc<ProductEvent, ProductState> {
final GetFeaturedProductsUseCase getFeaturedProducts;
ProductBloc({required this.getFeaturedProducts}) : super(ProductInitial()) {
on<FetchProductsEvent>((event, emit) async {
emit(ProductLoading());
final result = await getFeaturedProducts(NoParams());
result.fold(
(failure) => emit(ProductError(message: failure.message)),
(products) => emit(ProductLoaded(products: products)),
);
});
}
}
2. Dependency Injection with GetIt & Injectable
To decouple layers, use get_it for service location and injectable for automated code generation:
final sl = GetIt.instance;
void initDependencyInjection() {
// BLoC
sl.registerFactory(() => ProductBloc(getFeaturedProducts: sl()));
// UseCases
sl.registerLazySingleton(() => GetFeaturedProductsUseCase(repository: sl()));
// Repositories
sl.registerLazySingleton<ProductRepository>(
() => ProductRepositoryImpl(remoteDataSource: sl()),
);
}
Conclusion
Adopting Clean Architecture ensures that Flutter codebases remain testable, maintainable, and adaptable as business requirements grow.