Skip to main content

Building a Modular Project with a Monorepo Using Pub Workspaces Feature

Flutter™ projects often start small, but as they scale, managing multiple packages becomes challenging. If you find yourself working with several modules or features in one project, organizing them efficiently is crucial. Enter Monorepos with Pub Workspaces – an excellent solution for managing a Flutter project with multiple packages in a modular way.

In this post, we’ll explore how to structure a Flutter project in a modular way using a monorepo, and how to manage dependencies across various sub-projects without worrying about version conflicts. We’ll also look at how to configure Pub Workspaces to simplify dependency resolution and management.

When working with Flutter™ and Dart™, structuring a project in a modular way can improve scalability and maintainability. This is especially important when managing multiple packages in a single repository, also known as a monorepo. Pub Workspaces, introduced to simplify dependency management, provide a robust solution for such scenarios.

Why Use a Monorepo for Flutter Projects?

A Monorepo is a version-controlled code repository that holds multiple projects, which can be managed as part of a single codebase. This structure is beneficial when working on large projects where different features or modules share common code. By using a Monorepo, you can:

  • Encourage code reuse: Shared utilities, themes, or services can be used across different modules.
  • Maintain consistency: Ensures the use of the same version of a package across different features.
  • Improve collaboration: Teams can work on different parts of the application without worrying about the coordination of dependencies.

With Flutter, you can achieve this modularity and shared management using Pub Workspaces.

Project Structure for Modular Monorepo Using Pub Workspaces

Let’s break down the structure of a typical Flutter monorepo project using Pub Workspaces:



  • my_project/: The main project directory containing the primary pubspec.yaml, which lists all the sub-projects in the workspace.
  • core/: Contains foundational functionality used across all features. It can have utilities, constants, etc.
  • features/: These directories contain specific feature modules like code_push, l10nr, etc. Each module has its own pubspec.yaml for defining dependencies specific to that feature.
  • shared/: This folder contains components, localization, shared packages, and preferences, used across multiple features.
  • The pubspec.yaml in the Root Project

    In the main pubspec.yaml of my_project, define the workspace structure:









    This configuration centralizes the management of all subprojects and ensures they are part of the workspace. Additionally, it creates a single pubspec.lock file in the root of the project, ensuring consistent dependency versions across all subprojects.

    Add resolution: workspace to the pubspec.yaml of every subproject. For example:


    Key Concepts for Dependency Management

    In a modular Flutter project with Pub Workspaces, dependencies can be managed more effectively. Here’s how:

    1. Adding Dependencies Across Subprojects:

      • When a feature or module needs to depend on the core package, you simply add the core module as a dependency in that feature's pubspec.yaml.
      • For example, if your code_push feature needs to use the core package, you don’t need to define a version number or path; you can refer to it  within the workspace.
        This is the beauty of Pub Workspaces! You don’t need to specify version numbers and path for dependencies within the workspace; Flutter resolves them automatically and ensures consistency across the workspace.

        Benefits of Using Pub Workspaces in Flutter

         Monorepos

        1. No Version Conflicts: With Pub Workspaces, you don’t need to worry about version mismatches between your dependencies. All modules in the workspace use the same version.
        2. Centralized Dependency Management: All dependencies are handled within each subproject’s pubspec.yaml.
        3. Improved Code Sharing: The modular nature allows you to keep shared functionality (like utilities or services) in a single, reusable package (core), reducing redundancy.
        4. Scalability: As your project grows, you can add more features or modules easily without cluttering the main project, ensuring scalability and maintainability.

    View [WIP] Project Repo on GitHub

    Comments

    Contact:

    Popular posts from this blog

    Flutter™ Installation Without Android Studio™

    When developing with Flutter™, you typically install Android Studio™ as part of the setup process. However, if you want a lightweight alternative, you can install Flutter™ with only the Android command-line tools. This guide walks you through the steps to set up a performant, studio-free development environment on Windows. Table of Contents 1. Install Command-Line Tools 2. Install SDK Components 3. Set Environment Variables 4. Configure Flutter™ 1. Install Command-Line Tools First, download the Android SDK Command-Line Tools from the official Android Developer website . To ensure proper recognition by the Flutter SDK, extract the zip file following this directory structure: 📂 Path Structure Copy C:\Users\ \AppData\Local\Android\Sdk\cmdline-tools\latest\bin 2. Install SDK C...

    Android CLI: Beyond the IDE

    Agent-First Workflows Android CLI Command Reference The Android CLI is a unified developer interface architected to standardize environment control, workspace orchestration, and execution capabilities for standalone developer loops and automated AI agents. It serves as an optimized runtime engine connecting local projects directly to official tools, execution layers, and the centralized Android Knowledge Base . Quick Navigation Index Setup & Init Telemetry Scopes Global Option Flags Workspace Scaffolding Agent Systems Device & Vision Layers Toolchain & SDK IDE Deep Integration Installation & Global Setup Follow these structural phases to establish the system binary paths and expose core system hooks to target development execution layers. Phase 1 Bina...