How To Manage A Flutter Monorepo With Multiple Packages

Flutter category image

Introduction

Here is how to manage a Flutter monorepo with multiple packages and melos instead of using shell scripts. Repository administration is really easy with this field-tested tool!

I have some private Flutter packages in single repositories on GitHub. And because they are private, I had some authentication issues with transitive dependencies. But then someone guided me into another direction → monorepo. Here is a quick start guide of how to manage a Flutter monorepo with multiple packages and melos.

To be honest, I haven’t done that for years because it’s way easier to manage small repos. But thanks to melos, the effort is actually not that high for me. It only took me around an hour of learning and setup. However, I can add new packages without having to change anything in the configuration. It works out of the box.

In this article, I want to give you a short introduction of how I manage my monorepo with melos.

My goal

For my purpose, I basically have this one goal

New code must not break anything

Changes come via pull requests and can only be merged when the quality checks pass. Here is what I am aiming for

  • All tests pass (means running flutter test in every package)
  • All analyzers pass (means running flutter analyze in every package)

I could write a shell script for that and it would be easy. But if the repo grows I might run into problems in the future. That’s why I tried melos.

What is melos?

Melos is a tool used for handling Dart projects that contain multiple packages, often called mono-repos. It is currently being developed and is used in projects like FlutterFire.

Melos lets multiple packages operate within a single repository while still keeping them independent from each other. Its features include:

  • Automatically creating new versions and changelogs.
  • Automatically publishing packages to pub.dev.
  • Connecting and setting up local packages.
  • Running commands at the same time across different packages.
  • Showing a list of local packages and what they depend on. Melos is also very useful in CI/CD environments for automating complex tasks and solving challenges.

Check out the website for more details. They even support IDE integrations.

Melos setup

  1. Install melos
    Run dart pub global activate melos and make sure to add it to your PATH variable so that you can access it from anywhere with your preferred CLI tool.
  2. Create a pubspec.yaml in your root folder of your repository
    Set the name and environment properties (see below)
  3. Add a melos dev dependency You can do this with the command dart pub add melos --dev.
YAML
name: a_project

environment:
  sdk: '>=3.1.3 <4.0.0'
  
dev_dependencies:
  melos: ^6.0.0

4. Create a melos.yaml and configure your workspace
Tell melos about your repository structure like this so that it can find your packages.

YAML