Sub topic 13
Using Git and Path Dependencies
Assume you know basic Dart syntax, can run Flutter commands in a terminal, and understand YAML indentation. This chapter explains how to use a package when the version on pub.dev does not fit your project.
Most Flutter packages come from pub.dev. A hosted dependency is convenient because Pub downloads a published version and resolves it with the rest of your dependency graph. Sometimes you need an unpublished change, a local package under development, or a fork of an existing package. Git and path dependencies solve these cases.
Choosing a dependency source
A path dependency points to a package directory on your computer. It is useful when two packages are developed together, or when you want to test a local package before publishing it.
A Git dependency points to a Git repository. You can use a branch, tag, or commit. Git dependencies are useful for an unpublished package or a fork containing a fix.
| Dependency | Best use | Main risk |
|---|---|---|
| Hosted | Stable published package | The needed change is not published |
| Path | Local package development | It may not exist on another developer's computer |
| Git | Fork or unreleased package | A branch can change unexpectedly |
Adding a path dependency
Suppose your Flutter application and package have this structure:
workspace/
shop_app/
pubspec.yaml
shared_widgets/
pubspec.yamlFrom shop_app, the path to the package is ../shared_widgets. Add the package under dependencies:
dependencies:
flutter:
sdk: flutter
shared_widgets:
path: ../shared_widgetsThe directory must contain its own valid pubspec.yaml, including a package name that matches shared_widgets. Run dependency resolution from the application directory:
flutter pub getDart code imports the package by its declared package name, not by the directory name:
import 'package:shared_widgets/shared_widgets.dart';
class ProductTitle {
String text(String productName) {
return 'Product: $productName';
}
}A path dependency is not portable by itself. Every developer and build system must have the package at the same relative location. Keep related repositories in a predictable workspace, or use a hosted or Git dependency for shared builds.
Adding a Git dependency
Use a Git URL when the package is stored in a repository. The ref selects the branch, tag, or commit:
dependencies:
shared_widgets:
git:
url: https://github.com/example/shared_widgets.git
ref: mainA branch such as main can receive new commits. For repeatable builds, prefer a release tag or a full commit hash:
dependencies:
shared_widgets:
git:
url: https://github.com/example/shared_widgets.git
ref: 4f8c2a1d7b0e9c6a5f1234567890abcdef123456The repository must contain a Dart package with a compatible pubspec.yaml. If the package is inside a repository subdirectory, specify that directory:
dependencies:
shared_widgets:
git:
url: https://github.com/example/monorepo.git
ref: v2.1.0
path: packages/shared_widgetsAfter changing the source, run:
flutter pub getPub records the selected source and resolved revision in pubspec.lock. Commit the lockfile for an application so other builds use the same revision.
When to use overrides
A dependency override temporarily forces Pub to use a particular source or version, even when another package requests something else:
dependency_overrides:
shared_widgets:
path: ../shared_widgetsUse overrides for local testing, not as a permanent way to hide incompatible requirements. An override can make your application work while leaving another package untested against the selected version. Remove the override before publishing or creating a release unless it is an intentional part of the build.
Exercise
Which dependency source is most suitable for testing a package stored beside your Flutter app, and what limitation must you remember?
Show answer
Use a path dependency. The package must exist at the expected relative path on every development and build machine.