Make your Private Flutter Package

Hussain Habibullah
Flutter Community
Published in
4 min readApr 11, 2022

I am not against open-source, let me explain!!

Private Flutter Package

Flutter is an open-source UI toolkit and there are multiple open packages available on the pub.dev, which truly makes the developer’s life easier. But sometimes, due to the organization and product requirements, you have to create a private flutter package that might contain a separate application module, business logic, encryption algorithms, premium assets, confidential payment gateways, etc that you won’t like to publish publicly.

Hence, there could be multiple reasons to create a private flutter package and not publish it to the pub.dev, and it’s important for every flutter developer to know “How to create a private/unpublished flutter package”. Let’s dive deeper and learn how that works!

Out of Context - Did you know?

Once a package has been published to pub.dev, it cannot be unpublished or deleted.

Creating a Flutter Package

To create a flutter package, open your IDE and create a new flutter project.

Creating a new flutter project

The only difference is to select Project Type as Package this time.

In your package pubspec.yaml file, add publish_to: none to prevent publishing.

Using Private Flutter Package locally

In your main flutter project, you can refer to the package local directory like this:

But this is not the ideal case, as the package is hosted in your local directory and your colleagues won’t be able to collaborate on it. The ideal way would be to host it on a private git repository.

Hosting Private Flutter Package on Private Git Repository

Create a private git repository, and push your package source code there.

Creating a private repository
Push package source code to a private repo

Once done, it’s time to refer to this git-hosted package instead of the local directory.

Copy SSH URL of private repo

Copy the SSH URL of the repository and refer it to your main flutter project pubspec.yaml like this:

Almost done, you have successfully referred the private package repository to your pubspec, but we haven’t proved our access to the repository yet. If you run flutter pub get right now, it would display the following error, which is justified.

fatal: Could not read from remote repository.Please make sure you have the correct access rights
and the repository exists.
exit code: 128

Access private flutter package hosted on Git

You and every collaborator that wishes to use this private package need to do the following steps to prove his access to the private repository. (Make sure to add him as a collaborator first ;p)

  • Open Git Bash and generate SSH Key for the email, using this cmd:

ssh-keygen -t ed25519 -C “mail@gmail.com”

Press enter thrice to continue with default settings.

  • Run this cmd to ensure SSH Agent is running:

eval “$(ssh-agent -s)”

  • Add your SSH key to SSH Agent by running this command:

ssh-add ~/.ssh/id_ed25519

  • Now run this last command to copy your SSH public key to the clipboard.

clip < ~/.ssh/id_ed25519.pub

Your public SSH key is now copied, just paste it to your Github account > Settings > SSH & Deploy Keys > New SSH Key

Adding Public SSH key to Github Account (1)
Adding Public SSH key to GitHub Account (2)
Adding Public SSH key to GitHub Account (3)

Once your SSH key is added, try running flutter pub get again and this time it shouldn’t fail.

If it fails, you can run this command to add GitHub to the list of known hosts:

ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts

Wallah! It’s done! Thank you for reading and following along. That’s how you can make the private flutter packages that only you or your collaborators can access.

That’s it for today, hope you learned something from this article.

I am open to freelance, full-time, or part-time roles, feel free to reach out at Linkedin, thank you!

--

--