Menu

Dependency Management

Relevant source files

This page documents how dependencies are managed across the Flutter mobile project template. It covers the monorepo structure, package organization, dependency resolution, and tools used for efficient dependency management. The project uses Melos, a powerful tool designed for managing Dart/Flutter projects with multiple packages.

Monorepo Structure and Package Organization

The project is organized as a monorepo containing multiple packages, with dependencies flowing from apps to features to core packages.

Sources: pubspec.yaml5-26

The template workspace defines all packages through the workspace section in the root pubspec.yaml. This is how Melos knows which packages to manage:

Sources: apps/app/pubspec.yaml13-42 packages/features/debug_mode/pubspec.yaml12-34 packages/cores/data/pubspec.yaml12-22

Melos for Dependency Management

Melos is the primary tool used for dependency management across the monorepo. It's configured in the root pubspec.yaml file.

Sources: pubspec.yaml40-226

Melos provides shared dependency management, ensuring all packages use compatible versions of common dependencies. The bootstrap section defines shared dependencies that will be used across all packages:

CategoryImportant Dependencies
Shared Dependenciesflutter_hooks (^0.20.5), flutter_riverpod (^2.6.1), freezed_annotation (^2.4.4), hooks_riverpod (^2.6.1), go_router (^14.6.3), etc.
Shared Dev Dependenciesbuild_runner (^2.4.14), custom_lint (^0.7.0), freezed (^2.5.7), riverpod_generator (^2.6.3), etc.

Sources: pubspec.yaml52-72

Workspace Resolution

The project uses the resolution: workspace directive in package-level pubspec files to ensure consistent dependency versions across the monorepo.

Sources: apps/app/pubspec.yaml10 packages/cores/data/pubspec.yaml10 packages/features/debug_mode/pubspec.yaml10

When a package uses resolution: workspace, it will resolve dependency versions according to the workspace definition rather than independently, ensuring consistent dependency versions across all packages.

Package Dependency Structure

Packages reference each other using relative path dependencies. This ensures that changes in one package are immediately available to dependent packages without needing to publish them.

App Dependencies

The main app depends on core and feature packages:

dependencies:
  cores_core:
    path: ../../packages/cores/core
  cores_data:
    path: ../../packages/cores/data
  # ... other core packages
  features_debug_mode:
    path: ../../packages/features/debug_mode
  features_force_update:
    path: ../../packages/features/force_update
  # ... other feature packages

Sources: apps/app/pubspec.yaml13-30

Feature Package Dependencies

Feature packages depend on core packages and sometimes other feature packages:

dependencies:
  cores_core:
    path: ../../cores/core
  # ... other core packages
  features_force_update:
    path: ../force_update
  features_maintain:
    path: ../maintain
  # ... other dependencies

Sources: packages/features/debug_mode/pubspec.yaml12-34

Dependency Commands and Workflows

Melos provides several commands for managing dependencies across all packages:

Sources: pubspec.yaml75-83 pubspec.yaml122-135

Common Workflow Commands

CommandDescription
melos bootstrapInitialize dependencies across all packages
melos run upgradeUpdate dependencies to latest compatible versions
melos run upgrade:majorUpdate dependencies including major version changes
melos run genGenerate code (build_runner, localizations)
melos run fixApply automated fixes to code
melos run testRun tests across all packages

Sources: pubspec.yaml74-226

Dependency Override Management

The project template provides a mechanism for dependency overrides at the workspace level:

dependency_overrides:
  file: ^7.0.1
  path: ^1.9.1

Sources: pubspec.yaml36-38

This allows for forcing specific versions of problematic dependencies across all packages, which is especially useful when dealing with incompatible transitive dependencies.

Common Package Dependencies

Most packages in the project share common dependencies that are standardized through Melos:

Package TypeCommon Dependencies
All packagesflutter, flutter_riverpod, riverpod_annotation
UI packagesflutter_hooks, hooks_riverpod, cores_designsystem
Data packagesdio, cores_model, cores_core
Feature packagesRelevant core packages, flutter_localizations, intl

Sources: packages/cores/data/pubspec.yaml12-22 packages/features/setting/pubspec.yaml12-28 packages/cores/core/pubspec.yaml12-20

Getting Started with Dependency Management

To begin working with this template and manage dependencies:

  1. Ensure you have Melos installed:

    dart pub global activate melos
    
  2. Bootstrap the project to install all dependencies:

    melos bootstrap
    
  3. When adding new dependencies to a package, update the package's pubspec.yaml and then run:

    melos bootstrap
    
  4. To update dependencies across all packages:

    melos run upgrade
    

Remember to keep the root Melos configuration updated as you add new packages to the monorepo.

Sources: pubspec.yaml40-72