Sub topic 13
Using git and path dependencies
Hosted packages from pub.dev cover most needs. Sometimes you need code that isn't published there: a fix you haven't yet upstreamed, a package shared inside your company, or a library you are building next to your app. Git and path dependencies cover those cases. This chapter assumes you can edit pubspec.yaml, know what pubspec.lock records, and can read caret constraints such as ^1.2.0. It also assumes basic git use (branches, tags, commit hashes).
Where these dependencies fit
A dependency in pubspec.yaml has a source. Until now every package came from the hosted source, pub.dev. Pub supports two more sources:
- path: a package in a directory on your disk.
- git: a package in a git repository, which can be public or private.
The package still needs its own pubspec.yaml with a name. Once resolved, you import it with package: as usual. Only the way pub finds the code changes.
Path dependencies
A path dependency points at a directory that contains a package.
dependencies:
flutter:
sdk: flutter
my_local_package:
path: ../my_local_packageThe key under dependencies must match the name field in the target package's pubspec.yaml. If it doesn't, flutter pub get fails with a name mismatch error.
A minimal local package looks like this:
name: my_local_package
description: Shared formatting helpers.
version: 0.0.1
environment:
sdk: ^3.0.0// ../my_local_package/lib/my_local_package.dart
String shout(String input) => '${input.toUpperCase()}!';Use it from the app:
import 'package:my_local_package/my_local_package.dart';
void main() {
print(shout('hello'));
}How path dependencies behave
- Pub does not copy the package. It reads the files in place, so an edit to
../my_local_package/lib/is visible to the app after a hot restart. - If you change the local package's own
pubspec.yaml, for example by adding a dependency, runflutter pub getagain in the app. - Pub still resolves the local package's dependencies together with yours. Version constraints on both sides must be satisfiable.
- Prefer relative paths. An absolute path such as
/Users/ana/code/my_local_packageworks on one machine and breaks on every other machine and in CI.
A common use: a mono-repo
Path dependencies suit a repository holding an app and several internal packages:
dependencies:
core:
path: ../packages/core
design_system:
path: ../packages/design_systemThe packages ship with the app in one commit. You can change a package and the app that uses it in a single change, with no publishing step.
Git dependencies
A git dependency points at a repository. The full form is:
dependencies:
my_forked_package:
git:
url: https://github.com/example/my_forked_package.git
ref: mainIf you only need the default branch, a shorthand works:
dependencies:
my_forked_package:
git: https://github.com/example/my_forked_package.gitThe three keys
| Key | Meaning | Default |
|---|---|---|
| `url` | Repository address. HTTPS or SSH (`[email protected]:org/repo.git`). | Required |
| `ref` | A branch, tag, or commit hash. | The repository's default branch |
| `path` | A subdirectory inside the repository that holds the package. | The repository root |
Use path when the repository holds several packages or the package lives in a subfolder:
dependencies:
shared_widgets:
git:
url: [email protected]:example/mobile-libs.git
ref: v2.3.0
path: packages/shared_widgetsThis path key is not the same as a path dependency. It selects a folder inside the cloned repository.
Choosing a ref
A branch name moves whenever someone pushes to it. A tag is meant to stay fixed but can be re-pointed. A full commit hash never changes.
dependencies:
my_forked_package:
git:
url: https://github.com/example/my_forked_package.git
ref: 3f2a9c1d8e4b7a60c5d1e92f4b8a7d3c6e0f1a25Use a commit hash or an immutable release tag for anything you ship. Use a branch only while you are actively working against it.
Private repositories
Pub shells out to the git command, so it uses whatever credentials your git already has. For private repositories:
- Locally, use an SSH URL with a key loaded in your agent, or a configured credential helper for HTTPS.
- In CI, provide a deploy key or token before running
flutter pub get.
Do not put a token in the URL inside pubspec.yaml. That file is committed and would leak the secret.
What the lockfile records
Both sources appear in pubspec.lock, but they are pinned differently.
For a git dependency, the lockfile stores the exact commit that ref resolved to:
my_forked_package:
dependency: "direct main"
description:
path: "."
ref: main
resolved-ref: "3f2a9c1d8e4b7a60c5d1e92f4b8a7d3c6e0f1a25"
url: "https://github.com/example/my_forked_package.git"
source: git
version: "1.4.0"Even with ref: main, flutter pub get keeps using resolved-ref until you ask for a newer commit:
flutter pub upgrade my_forked_packageThat command fetches the repository again and moves resolved-ref to the current tip of main. This is why committing pubspec.lock matters for app projects. Teammates and CI then build from the same commit, even if the branch has moved.
For a path dependency, the lockfile records only the location:
my_local_package:
dependency: "direct main"
description:
path: "../my_local_package"
relative: true
source: path
version: "0.0.1"There is no commit or archive to pin. Whatever is on disk is what you build.
Pub caches cloned repositories under the pub cache directory (~/.pub-cache/git on macOS and Linux). If that cache is corrupted, flutter pub cache repair re-downloads the packages it holds.
How versions work with these sources
Pub reads the version field in the dependency's own pubspec.yaml and uses it for solving. If another package in your tree requires my_forked_package: ^1.0.0, the version in the forked repository's pubspec must satisfy it, or resolution fails. When you fork a package, keep the version field in your fork compatible with what the rest of the tree expects. Otherwise you will see a version solving error that names both sides.
Limits to know about
- Publishing. pub.dev rejects a package that depends on git or path packages. An app is never published to pub.dev, so apps are fine. If you are writing a library for publication, replace these entries with hosted versions first.
- Reproducibility. A git dependency pinned to a branch changes underneath you on
pub upgrade. A path dependency can differ on each machine. Both need team discipline. - Availability. A git dependency fails to resolve if the repository is deleted or the host is unreachable. A hosted package is mirrored and retained by pub.dev.
- Maintenance. A fork does not receive upstream fixes. Track the upstream project, and plan to switch back to the hosted version once your fix is released.
Choosing between them
Common errors
- "Could not find package" or name mismatch. The key under
dependenciesmust equal thenamein the target's pubspec. - "Could not find a file named pubspec.yaml" for a git source. The package is in a subfolder. Add
path:undergit:. - Git authentication failures in CI. The CI user lacks a key or token. Fix it in CI settings, not in the URL.
- A path that works only on your machine. You used an absolute path. Change it to a relative one.
- Stale code from a branch. The lockfile still holds the old
resolved-ref. Runflutter pub upgrade <package>.
Exercise
Your app depends on a library in a private repository at [email protected]:acme/ui-kit.git. The package lives in the packages/buttons folder, and you want to lock it to release tag v1.4.0. Write the dependency entry, and say what pubspec.lock records for it.
Show answer
dependencies:
buttons:
git:
url: [email protected]:acme/ui-kit.git
ref: v1.4.0
path: packages/buttons
The key buttons must equal the name in packages/buttons/pubspec.yaml. The lockfile records source: git, the url, ref, path, and a resolved-ref holding the full commit hash that v1.4.0 pointed to when pub resolved it.
Exercise
A teammate pulls your changes and the app builds, but a bug you fixed in a git dependency tracking ref: main is still present for them. Name one likely cause and the command that fixes it.
Show answer
Their pubspec.lock still holds an older resolved-ref, or your lockfile with the new commit was not committed. Commit the updated lockfile after running flutter pub upgrade <package>. They then run flutter pub get. If they have only the old lockfile, they can run flutter pub upgrade <package> themselves.
Exercise
Why can't a library you plan to publish to pub.dev keep a path dependency on a sibling package?
Show answer
pub.dev rejects packages that depend on path or git sources, because those sources are not available to the people who install your package. You must publish the sibling package first and depend on it by a hosted version constraint.