Creating own Dart packages

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.

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

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.

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.

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.

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

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

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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store