The standard way of publishing a Flutter/Dart package is publishing it publicly on pub.dev, but what if you need to create a private package to reuse code in yours apps, or if a company wants to share common code between its proprietary apps?
- Same product: Keep packages in the same repository
- Company-wide: Use different Git repositories or pay for a private package repository server
The easier way is to create packages in the same repository of the app. It's very used to test a example app when creating packages and plugins. To add it to your app, just indicate the path to dependency:
dependencies:
plugin1:
path: ../plugin1/
Cons: To reuse packages in different apps, you would need to keep all the apps in the same repository.
Docs (flutter.dev)
You can add dependencies directly from its repositories
dependencies:
plugin1:
git:
url: git://github.com/private-repository/plugin1.git
Cons: You can't use semantic versioning to keep the app updated for bugfixes and previnting major versions with breaking changes. You must reference a specific branch or tag to add a specific package version, or refer to main branch to have an always up-to-date version, which can have breaking changes.
Docs (flutter.dev)
You can upload your packages privately to a package repository server
Options:
- CloudSmith (docs)
- JFrog ()
dependencies:
plugin1:
hosted: https://some-package-server.com
version: ^1.4.0
Cons: It's a payed service
Docs:
- Custom package repositories (dart.dev)
- Dependencies (dart.dev)
There isn't an official way of self-hosting currently.
pub.dev repository on GitHub provide a guide to run it as a standalone server, but a recent issue says it's outdated. There's also other official repositories, pub-dartlang and pub_server, which are deprecated.
ByteDance, owner of TikTok, provides a way to self-host a pub server with unpub, "a self-hosted private Dart Pub server for Enterprise, with a simple web interface to search and view packages information." I haven't tested it yet so I can't say if it's a good option.