A Step-by-Step Guide to Publishing Your Package on pub.dev

Saman KC
3 min readJun 20, 2023

--

pub.dev

Pub.dev, the official Dart and Flutter package repository, is a great place for developers to find, share, and publish packages. Publishing your own package on pub.dev allows other developers to easily integrate your code into their projects, boosting collaboration and code reuse. In this blog article, we will walk you through the process of publishing a package to pub.dev, ensuring that your package reaches a wider audience and contributes to the growing Dart and Flutter communities.

So, let’s get started.

First, let’s look at how to create a package. For that, head over to your terminal and type:

dart create -t package <PACKAGE_NAME>

where <Package_Name> refers to the name of your package. This will create the initial directory and structure for your package.

Now, set up the pubspec.yaml. This file is critical for specifying your package’s metadata and dependencies. Include the package name, version, description, homepage (URL referring to the homepage (or source code repository) of the package), environment, and any dependencies (optional).

Next, Code Development.

Start writing code in the lib directory. Consider code reusability and maintainability, adhere to style conventions, and use good coding practices. Write understandable, thoroughly documented code since consumers of your package will depend on it.

Create thorough tests to confirm your package’s functionality. You may assist guarantee that your code operates as expected and maintains stability between upgrades by using unit tests and integration tests.

Once the development of your code is complete, it’s time for you to document your package.

Now navigate to the README.md file located in the package’s root directory. The beginning of the package documentation starts with this file. Give an explanation of the purpose of your package, directions for installing it, sample code, and usage tips.

Using a version control system like Git is highly recommended to track changes and collaborate with others.

Preparing to publish

Start with including a LICENSE file in the root directory of the package. Dart recommends the BSD 3-clause license. However, feel free to use any license as you like for your package.

Next, if your package is larger than 100MB after zipping, then, remove unnecessary content by mentioning them to the .pubignore file.

Also, include a example folder in the root directory, which contains a code to demonstrate the usage of your package.

In the root directory, make a CHANGELOG.md file that lists the modifications made by the package. You can initially add features that the package offers.

Important files:

README.md
CHANGELOG.md
pubspec.yaml

Here’s how a complete package might look like:

package-name/
.dart_tool/ *
pubspec.yaml
pubspec.lock **
LICENSE
README.md
CHANGELOG.md
bin/
enchilada
doc/
api/ ***
getting_started.md
example/
main.dart
lib/
file.dart
src/
file.dart
test/
file_test.dart

Next, headover to pub.dev and create a account.

Now, to publish your package to pub.dev, first type the following command:

dart pub publish --dry-run

this will check and ensure that your package follows the pubspec format and the package layout conventions. If the command runs with 0 warnings then you are free to proceed to the next step. Here’s how it might will look like.

Publishing transmogrify 1.0.0
.gitignore
CHANGELOG.md
README.md
lib
transmogrify.dart
src
transmogrifier.dart
transmogrification.dart
pubspec.yaml
test
transmogrify_test.dart

Package has 0 warnings.

Now, you are ready to publish your package. For that, type:

dart pub publish

Congratulations🎉, you have now successfully published your package to pub.dev. Any Pub user can now download or depend on your package once it has been successfully uploaded and is available for use in their projects. Another Dart developer might include your package as a dependency in their pubspec.yaml.

--

--

Saman KC

A Passionate Flutter Developer | Creating seamless experiences with Flutter