Codenic Clean Architecture
  • Introduction
  • The Clean Architecture
    • Presentation Layer
    • Domain Layer
    • Infrastructure Layer
  • Tutorial
    • Overview
    • Creating a Flutter Modular Project
      • Tips for Managing a Modular Project
    • Implementing the Domain Layer
      • Core Dependencies
      • Creating an Entity
      • Creating a Failure
      • Creating a Repository Interface
      • Creating Use Cases
        • CRUD Operations (Runner)
        • Data Streams (Watcher)
    • Implementing the Infrastructure Layer
      • External Dependencies
      • Creating a Data Model
      • Creating a Data Source
      • Implementing a Repository
    • Implementing the Presentation Layer
      • External Dependencies
      • Dependency Injection and Service Locator
      • Widgets
        • Snackbar Handler
        • Global Blocs Widget
        • Note Widgets
  • Packages
    • Codenic Bloc Use Case
      • Runner
      • Watcher
    • Codenic Logger
      • Usage
      • Example
      • Modifying the Logger
      • Integrating Firebase Crashlytics to the logger
    • Codenic Exception Converter
      • Failure Object
      • Exception Converter
      • Exception Converter Suite
      • Example
Powered by GitBook
On this page
  • 1. Create the Flutter root project
  • 2. Create the submodules
  • 3. Interlink the submodules
  • 4. Create the submodule base directories and files
  1. Tutorial

Creating a Flutter Modular Project

PreviousOverviewNextTips for Managing a Modular Project

Last updated 1 year ago

The project will be organized into multiple modules to separate the different architectural layers of the application. This helps to ensure that the is not directly connected to the , and that all communication between these layers must go through the .

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/

3. Interlink the submodules

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
Presentation Layer
Infrastructure Layer
Domain Layer