Using Dart & Flutter Packages from a Private Git Repo
We want to use Dart/Flutter packages from a private git repo on Github.
The following article will show you how.
Set up SSH client
To authenticate ourselves into the private repo, we will need to use the SSH protocol.
Mac and Linux
Mac and Linux usually has an ssh client configured. If not present, you can install one with your system's a package manager (apt, dnf, pacman, brew etc..) .
Windows 10 has a built-in OpenSSH client. But it must be activated first. Check here: howtogeek.com/336775/how-to-enable-and-use-..
Powershell also has a built-in ssh client.
Set up SSH Keys on Github
We need to associate an SSH key to provide authentication to the private repo.
- Sign in to your github account that has access to the private repo
- Go to github.com/settings/keys and add your public SSH key into your account
Generating SSH Keys
If you don't have an SSH key, you can generate one. For simplicity's sake, we will create one that doesn't have a passphrase, as doing so would require extra work to make it automatic (setting up ssh-agent on Linux/Mac).
Mac and Linux and Windows using Powershell
ssh-keygenon your terminal
- Specify a name for this keypair, in this example we use the name
- For now skip the process where it asks for a passphrase
- This will create a two keys, a public key (
my_key.pub) and private key (
Set up SSH config on your local machine
.ssh folder, where your public and private keys are stored by default, lives a
This file will help our git client manage our connection to remote git hosts (like Github, Bitbucket, Gitlab etc) when we clone using SSH.
Here we can set it up to associate an Identity File (a private/pub key pair) to a specific host.
ssh/.config file. It can be found below:
Mac and Linux
Add the following snippet to your
# Using your company key Host github.com HostName github.com PreferredAuthentications publickey User git IdentityFile ~/.ssh/my_key
Hostcan mean anything. It's just used for us to separate between different accounts we use on, say, GitHub.
HostNameis the location of the remote git host in our case it's
PreferredAuthenticationstells your client to use public key authentication first, before password based authentication
Userhas to be git when authenticating with Github
IdentityFileis the location of your private key.
Check if you can clone the private repo
If you can clone the private repo, it means you are authenticated.
Use the package as a dependency
In your Flutter project, open the
dependencies, you can add your private flutter package located in your repository.
dependencies: my_test_package: git: url: firstname.lastname@example.org:my_company_repo/my_test_package.git
flutter pub get and the package should be available for use in the project.
Specifying a version
There's no direct versioning system when using a git package dependency. However, we can specify which branch, or tag, or commit hash to pull from.
dependencies: my_test_package: git: url: email@example.com:my_company_repo/my_test_package.git ref: master
ref property can have a branch, tag or a specific commit hash.
You can read more about this spec here: flutter.dev/docs/development/packages-and-p..