DEV Community

Jhin Lee
Jhin Lee

Posted on • Updated on

Start the flutter monorepo project

Basic recipe to start monorepo project for flutter

  1. Install melos (more details about melos: https://melos.invertase.dev/)

    dart pub global activate melos
    
  2. Create a project folder

    mkdir flutter_project
    cd flutter_project
    
  3. Add melos.yaml file

    name: flutter_skeleton
    
    packages:
      - packages/**
      - apps/**
    
    command:
      bootstrap:
        usePubspecOverrides: true
    
    scripts:
      analyze:
        exec: flutter analyze .
      outdated:
        exec: flutter pub outdated
      test:
        exec: flutter test
    
  4. Create .gitignore and add pubspec_overrides.yaml in it.

  5. Create the main app

    mkdir apps # create dir for applications
    cd apps
    flutter create main_app
    
  6. Add a library package

    cd .. # go back to the root dir of the project
    mkdir packages # create dir for shared packages
    cd packages 
    flutter create --template=package lib_example
    
  7. Bootstrap melos

    melos bs # melos bootstrap
    
  8. Try melos scripts added in melos.yaml file

    melos analyze # run analyze for all packages
    melos test # run test for all packages
    melos outdated # check outdated pub dependencies for all packages
    melos upgrade # Update dependencies for all packages
    

That's it! Now you have the monorepo project ready to start. You can open the root directory with vscode or IntelliJ(Or Android Studio). melos already bootstrapped the IntelliJ project. Nothing to do with vscode, but you can install the extension if it makes your life easier.

More information: https://melos.invertase.dev/ide-support

Use the shared package from the main app

Add the package name as a dependency in the pubspec.yaml

Something like:

dependencies:
  lib_example:
    path:../packages/lib_example
Enter fullscreen mode Exit fullscreen mode

You can indicate the package version instead of the path if you ever published the package to the pub.dev. I had to put path because flutter pub outdated doesn't respect the pubspec_overrides.yaml for the local dependency for the moment.

The complete code can be found on github

Top comments (0)