Creating own Dart packages

Thiago Evoá
3 min readJan 11, 2022

Hello dev, we all know that Flutter is in a very good hype and one way to see that is looking through the pub.dev website how many packages have been created every time, and for everything.

But it’s still possible that you need a package to share between your projects and you can’t find a good enough one or just don’t want to rely on a external package for many reasons, and because of that I want to talk about creating your own package.

So let’s create the project using the command like the image below, because this is how we create a package.

terminal

And if everything went well, you probably have something like this.

package

Now if your got curious, you probably took a look at the code inside the lib folder and saw just a increment function, but I’ll do a little thing to improve the package usage, and for this I’ll just export one file with the Calculator class witch contains the sample code.

calculator
export

So now it’s time to publish your code to the Github and let’s see how to import in other project. If you don’t know how to use Git, make sure to search about it.

In my case I created a Github repository and pushed the created project as the image bellow.

GitHub

So now let’s create a normal Flutter project and import the package. Note that the process to import the package is almost the same as usual, but instead you have to use the Github url.

pubspec.yaml

Now it’s time to use it. And for better understanding I’ll use the counter app just to continue incrementing it.

using package

And as the result we have something like the gif below.

counter_app

And that’s it, if you need for any reason making a private package or share a package with the community with this simple process it’s going to be possible.

--

--