clean-code-architecture-flutter
Seed app with clean code architecture
In clean code architecture, there are three main layers
- Data
- Domain
- Presentation along with
- common (can also be called as core)
Common
This contains all the common functionalities for the application. e.g http client, env config, platform specific stuffs like network checker, etc.
These common functionalities will be used by data layer, domain layer and presentation layer e.g http client by remote data source, network info by repository layer, enums by domain layer and config by presentation.
Domain
This is innermost layer and will contain the core business logic i.e. usecases and business object also called as entities, the usecase depends on the contract of repository (not implementation)
Data
The data layers contains the repository implementation and this layer is closest to actual data sources and is responsible for communication with data sources.
The data layer returns the models and not entities and the model also contains the fromJson and toJson mapper.
Note: We donโt have an extra mapper class, we are just going to leverage the named constructor by dart to map our data set from one form to our models. toJson and fromJson.
Presentation
This layer contains all the information about the UI and everything to show to the end user customer. The presentation layer also contains the bloc which is the state management and often termed as brain of UI.
The presentation layer interact via domain layer, i.e entities and use cases.
Folder structure
โโโ lib
โ โโโ common
โ โ โโโ configs
โ โ โ โโโ configuration.dart
โ โ โโโ constants
โ โ โ โโโ exception_constants.dart
โ โ โ โโโ http_constants.dart
โ โ โ โโโ local_database_type_constants.dart
โ โ โ โโโ route_constants.dart
โ โ โโโ exceptions
โ โ โ โโโ bad_request_exception.dart
โ โ โ โโโ forbidden_exception.dart
โ โ โ โโโ server_error_exception.dart
โ โ โ โโโ server_exception.dart
โ โ โ โโโ unauthorized_exception.dart
โ โ โโโ extensions
โ โ โ โโโ string_extension.dart
โ โ โโโ http
โ โ โ โโโ http_client.dart
โ โ โโโ injector
โ โ โ โโโ injector.dart
โ โ โ โโโ injector_config.dart
โ โ โ โโโ injector_config.g.dart
โ โ โโโ utils
โ โ โโโ database_util.dart
โ โ โโโ http_utils.dart
โ โโโ data
โ โ โโโ datasources
โ โ โ โโโ local
โ โ โ โ โโโ local_database
โ โ โ โ โ โโโ base_local_database.dart
โ โ โ โ โ โโโ tables
โ โ โ โ โ โ โโโ todo_table.dart
โ โ โ โ โ โ โโโ todo_table.g.dart
โ โ โ โ โ โโโ todo_local_datasource.dart
โ โ โ โ โโโ shared_preference
โ โ โ โโโ remote
โ โ โ โโโ constants
โ โ โ โ โโโ todo_remote_datasource_constants.dart
โ โ โ โโโ todo_remote_datasource.dart
โ โ โโโ models
โ โ โ โโโ todo_model.dart
โ โ โโโ repositories
โ โ โโโ todo_repository_impl.dart
โ โโโ domain
โ โ โโโ entities
โ โ โ โโโ todo_entity.dart
โ โ โโโ repositories
โ โ โ โโโ todo_repository.dart
โ โ โโโ usescases
โ โ โโโ todo_usecase.dart
โ โโโ main.dart
โ โโโ presentation
โ โโโ app.dart
โ โโโ common_bloc
โ โโโ journey
โ โ โโโ dashboard
โ โ โ โโโ dashboard_constants.dart
โ โ โ โโโ dashboard_routes.dart
โ โ โ โโโ dashboard_screen.dart
โ โ โโโ todo
โ โ โโโ bloc
โ โ โ โโโ todo_bloc.dart
โ โ โ โโโ todo_event.dart
โ โ โ โโโ todo_state.dart
โ โ โโโ create_todo
โ โ โ โโโ create_todo_constants.dart
โ โ โ โโโ create_todo_screen.dart
โ โ โโโ todo_list
โ โ โ โโโ todo_list_screen.dart
โ โ โ โโโ widgets
โ โ โ โโโ todo_item.dart
โ โ โ โโโ todo_list_constants.dart
โ โ โโโ todo_routes.dart
โ โโโ routes.dart
โ โโโ themes
โ โ โโโ custom_icons.dart
โ โ โโโ theme_colors.dart
โ โ โโโ theme_data.dart
โ โ โโโ theme_text.dart
โ โโโ widgets
โโโ pubspec.lock
โโโ pubspec.yaml
โโโ test
โโโ __setup__
โ โโโ base_test_bloc.dart
โ โโโ navigation_mock.dart
โ โโโ path_provider_mock.dart
โ โโโ wrapper.dart
โโโ common
โ โโโ exceptions
โ โ โโโ bad_request_exception_test.dart
โ โ โโโ forbidden_exception_test.dart
โ โ โโโ server_error_exception_test.dart
โ โ โโโ unauthorized_exception_test.dart
โ โโโ http
โ โ โโโ __mock__
โ โ โ โโโ http_client_data.dart
โ โ โ โโโ http_client_mock.dart
โ โ โโโ http_client_test.dart
โ โโโ utils
โ โโโ __mock__
โ โ โโโ sample_adapter.dart
โ โ โโโ sample_table.dart
โ โ โโโ sample_table_mock.dart
โ โโโ database_utils_test.dart
โ โโโ http_util_test.dart
โโโ data
โ โโโ datasources
โ โ โโโ local
โ โ โ โโโ __mock__
โ โ โ โ โโโ base_local_datasource_mock.dart
โ โ โ โ โโโ sample_model.dart
โ โ โ โ โโโ todo_local_datasource_mock.dart
โ โ โ โ โโโ todo_table_data.dart
โ โ โ โโโ base_local_datasource_test.dart
โ โ โ โโโ todo_local_datasource_test.dart
โ โ โโโ remote
โ โ โโโ __mock__
โ โ โ โโโ http_client_mock.dart
โ โ โ โโโ todo_remote_datasource_mock.dart
โ โ โโโ todo_remote_datasource_test.dart
โ โโโ models
โ โ โโโ __mock__
โ โ โ โโโ todo_model_data.dart
โ โ โโโ todo_model_test.dart
โ โโโ repositories
โ โโโ __mock__
โ โ โโโ todo_json_data.dart
โ โ โโโ todo_repository_mock.dart
โ โโโ todo_repository_impl_test.dart
โโโ domain
โ โโโ entities
โ โ โโโ __mock__
โ โ โโโ todo_entity_data.dart
โ โโโ usecases
โ โโโ __mock__
โ โ โโโ todo_usecase_mock.dart
โ โโโ todo_usecase_test.dart
โโโ local_database_test_files
โ โโโ database_util_test.hive
โ โโโ database_util_test.lock
โ โโโ testbox.hive
โ โโโ testbox.lock
โ โโโ todo.hive
โ โโโ todo.lock
โโโ presentation
โโโ app_test.dart
โโโ journey
โโโ dashboard
โ โโโ dashboard_screen_test.dart
โโโ todo
โโโ bloc
โ โโโ __mock__
โ โ โโโ todo_bloc_mock.dart
โ โโโ todo_bloc_test.dart
โโโ create_todo
โ โโโ create_todo_screen_test.dart
โโโ todo_list
โ โโโ todo_list_screen_test.dart
โ โโโ widgets
โ โโโ todo_item_test.dart
โโโ todo_routes_test.dart
How to run
flutter run
In case of addition to injector file,
run flutter pub run build_runner build --delete-conflicting-outputs
How to test
flutter test --coverage
To see percentage
lcov -r coverage/lcov.info "*/__test*__/*" "*.g.dart" -o coverage/lcov_cleaned.info
To see coverage of each file in html view
genhtml coverage/lcov_cleaned.info -o coverage