Creating a private package on Flutter

Thiago Evoá
4 min readDec 18, 2024

--

Hello devs, sometimes we need to make usage of a package and for that we have a bunch of them on pub.dev, but what about the necessity on your project of having a private one for whatever reason it might be?

How can we create a package that is going to be used just by yourself and what about the processes of doing it… is it easy? Let's see on this tutorial how to achieve this goal.

So… I would start thinking of where the package is going to be stored since we are not going to use the pub.dev repository, and for the that let's just use the Github because everyone already have it right?

Create a new repository on the Github page according to the image below.

new repository

And you should have something like this afterwords.

github repository

Now on your terminal run the command below and you should have a similar thing as the image.

flutter create --template=package hello_world_package  
create package

We already have the package project and before push it to the remote repository let's just do a couple o things.

Inside the project go to the pubspec.yaml and give a good description so it can tell to the user the purpose of it. Then add the url from the Github repository you just created on the homepage: as the image below.

pubspec.yaml

Go to the CHANGELOG.MD file and add a description of the change you are going to release on this version of the package.

changelog.md

On the hello_world_package.dart declare it as a library but do not explicity give it a name since is no longer recommended as a good practice, check this Dart good practices page.

hello_world_package.dart

And create the Dart file with a get method with the Hello World string as a return.

hello_world.dart

Last but not least, let's create a test as an example because we devs write test right? Right?

hello_world_package_test.dart

And let's push the package to the Github repository by running these commands below on the root of the project.

git init
git remote add origin <repository url>
git add .
git commit -m "Creating my first package"
git branch -M main
git push -u origin main

And you should have something like this.

push to github
package repository

To make usage of the package that you just did, create a Flutter project and add to your pubspec.yaml the dependency like so.

pubspec.yaml

And on the code just use as usual.

hello world

And the result should be like this:

app

The purpose of this tutorial is about understanding how to create a private Flutter package in a simple way. If you consider doing it do distribute elsewhere, bare in mind that you need to follow more steps that would make the package on a professional level, like adding licence and more. For that go to the develop package's official page and read carefully the standards you should follow.

--

--

Thiago Evoá
Thiago Evoá

Written by Thiago Evoá

A simple Flutter dev learning everyday.

No responses yet