Comment on page
Creating a Flutter Modular Project
The project will be organized into multiple modules to separate the different architectural layers of the application. This helps to ensure that the Presentation Layer is not directly connected to the Infrastructure Layer, and that all communication between these layers must go through the Domain Layer.
flutter create note_app --org=com.example
Create a folder called
modules
in the root of the project. Inside this folder, create the following submodules:flutter create presentation --template=module --org=com.example
flutter create domain --template=module --org=com.example
flutter create infrastructure --template=module --org=com.example
Make sure that the root project and submodule organization prefixes (--org) are the same.
After creating the modules, your project should look something like this:
root/
└── modules/
├── domain/
├── infrastructure/
└── presentation/
Specify the interconnections between submodules by importing the necessary modules for each
pubspec.yaml
file.The domain does not depend on any other modules. Nonetheless, we can simplify the
note_app/modules/domain/pubspec.yaml
: name: domain
description: The domain layer of the application.
version: 1.0.0+1
publish_to: none
environment:
sdk: '>=3.0.6 <4.0.0'
dependencies:
flutter:
sdk: flutter
flutter:
module:
androidX: true
androidPackage: com.example.domain
iosBundleIdentifier: com.example.domain
Add the
domain
submodule to the note_app/modules/infrastructure/pubspec.yaml
:name: infrastructure
description: The infrastructure layer of the application.
version: 1.0.0+1
publish_to: none
environment:
sdk: '>=3.0.6 <4.0.0'
dependencies:
flutter:
sdk: flutter
domain:
path: ../domain
flutter:
module:
androidX: true
androidPackage: com.example.infrastructure
iosBundleIdentifier: com.example.infrastructure
Add the
domain
submodule to the note_app/modules/presentation/pubspec.yaml
:name: presentation
description: The presentation layer of the application.
version: 1.0.0+1
publish_to: none
environment:
sdk: '>=3.0.6 <4.0.0'
dependencies:
flutter:
sdk: flutter
domain:
path: ../domain
flutter:
uses-material-design: true
module:
androidX: true
androidPackage: com.example.presentation
iosBundleIdentifier: com.example.presentation
Add all the submodules to the root
note_app/pubspec.yaml
:name: note_app
description: The root module of the application.
publish_to: none
version: 1.0.0+1
environment:
sdk: '>=3.0.6 <4.0.0'
dependencies:
flutter:
sdk: flutter
presentation:
path: modules/presentation
domain:
path: modules/domain
infrastructure:
path: modules/infrastructure
flutter:
uses-material-design: true
Before proceeding, delete the default
main.dart
and widget_test.dart
in each submodule as they will not be needed.Once done, apply the following base folders and files per module:
root/
└── modules/
└── domain/
└── lib/
├── note/ # feature directory
| ├── entities/
| ├── failures/
| ├── repositories/
| ├── use_cases/
└── domain.dart # barrel file
root/
└── modules/
└── infrastructure/
└── lib/
├── data_sources/
├── note/ # feature directory
└── infrastructure.dart # barrel file
root/
└── modules/
└── presentation/
└── lib/
├── note/ # feature directory
└── presentation.dart # barrel file
Last modified 4mo ago