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:

3.2. Update the Infrastructure pubspec.yaml

Add the domain submodule to the note_app/modules/infrastructure/pubspec.yaml:

3.3. Update the presentation pubspec.yaml

Add the domain submodule to the note_app/modules/presentation/pubspec.yaml:

3.4. Update the main pubspec.yaml

Add all the submodules to the root note_app/pubspec.yaml:

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

4.2. Create the files and folders in the infrastructure submodule

4.3. Create the files and folders in the presentation submodule

Last updated