Dart packages can be published to pub.dev and it’s a pretty simple process.
- 1. Sign up to create an account on pub.dev
- 2. Don’t worry about the Publisher, you can create and upload packages without this
Next up, let’s assume you don’t yet have a package yet – check out Creating packages to learn about the expected project layout of folders/files. We can simply run the following Dart command to create the template for us
dart create -t package-simple mypackage
Obviously change the mypackage name to that of your package.
Before we look into the requirements for publishing, let’s just state that pub.dev (when you publish your package) runs some analysis across your package and assigns you PUB POINTS – for example to follow Dart file conventions you’re expected to supply a valid pubspec.yaml, a valid README.md and a valid CHANGELOG.md.
- Update the pubspec.yaml description field – ensure it has 60-180 characters
- The package created by the Dart CLI gives a good starting point for the README.md and CHANGELOG.md – don’t forget to update the CHANGELOG.md to match each version
- Implement your code in the lib/src folder – files should use lowercase, snakecase, or you’ll lose PUB POINTS
- Ensure you’re lib .dart file has
library mypackage;
Replacing mypackage with your package name if you change the package name at any point during development.
- Add your tests to the test folder
- Add an example to the example folder
- Ensure your code has doc comments to your lib .dart file as well as to the lib/src files, i.e.
/// My Doc Comments
Assuming you’ve got everything in place, run
dart format .
to adhere to Dart formatting preferences. You’ll lose PUB POINTS if you publish with format issues.
Ensure your tests all run successfully using
dart test
Run the publish process in dry-run mode using
dart pub publish --dry-run
When ready, run
dart pub publish
You can exclude files from publishing by prefixing the filenames with a ., any directory with the name packages is ignored. Finally, files and directories within .pubignore or .gitignore are also excluded.
If I recall when publishing, you may be asked to log into pub.dev – just follow the publish command’s prompts and if all went well you’ll have the package upload to pub.dev.
Remember, once you’ve published a package it cannot be deleted, but you can mark them as discontinued. So make sure you have your package named as your want it to be (at the very least) before you publish it.
Ensure versions are correct and updated each time you publish, along with CHANGELOG.md updates to show what happened for each new version.
Finally pub.dev will run it’s analyse process and then supply a PUB POINT score out of 130 and will give information on what’s missing or needs attention to get a 130/130 score – for example, my current package needs a couple of files formatting correctly and an example as I didn’t use the package template but wrote mine from scratch.
Lastly, try to get your README.md etc. right before publishing as you’ll have to increment the version of your app just to change the README.md (yes I learned the hard way).