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.

1. Create the Flutter root project

flutter create note_app --org=com.example

2. Create the submodules

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.

3.1. Update the domain pubspec.yaml

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

3.2. Update the Infrastructure pubspec.yaml

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

3.3. Update the presentation pubspec.yaml

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

3.4. Update the main pubspec.yaml

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

4. Create the submodule base directories and files

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:

4.1. Create the files and folders in the domain submodule

root/
└── modules/
└── domain/
└── lib/
├── note/ # feature directory
| ├── entities/
| ├── failures/
| ├── repositories/
| ├── use_cases/
└── domain.dart # barrel file

4.2. Create the files and folders in the infrastructure submodule

root/
└── modules/
└── infrastructure/
└── lib/
├── data_sources/
├── note/ # feature directory
└── infrastructure.dart # barrel file

4.3. Create the files and folders in the presentation submodule

root/
└── modules/
└── presentation/
└── lib/
├── note/ # feature directory
└── presentation.dart # barrel file