The Dart ecosystem
is a collection of packages and libraries
that you can import in your
Dart-lang based projects.
If you are creating a Flutter app,
you will have a
pubspec.yaml
file with the dependencies needed
to run it.
Publishing your own a package(s) is fairly simple. All packages are published to: pub.dev
There are some constraints that
need to be met before publishing a package
in Dart:
Firstly, the project should have a
README.md, a CHANGELOG.md and a
pubspec.yaml file
(which is used to fill out the details
about the package - these can be found
on the package's page,
like in the picture below).
The pubspec.yaml file
for the
dart_multihash
package is:
name: dart_multihash
description: A dart-lang implementation of the Multihash protocol.
version: 0.0.1
homepage: https://github.com/dwyl/dart_multihash
repository: https://github.com/dwyl/dart_multihash
issue_tracker: https://github.com/dwyl/dart_multihash/issues
environment:
sdk: '>=2.18.4 <3.0.0'
dependencies:
buffer: ^1.1.1
dev_dependencies:
lints: ^2.0.0
crypto: ^3.0.2
convert: ^3.1.1
test: ^1.16.0
collection: ^1.17.0The name, description, version,
homepage, repository, issue tracker,
amongst other properties effect the
sidebar of the official page of the package
after publishing.
If you have wondered about, you might have noticed that lots of packages are published by ...well, publishers.
In the previous picture,
regarding the dart_multihash package
you might have noticed that
there's a dwyl.com
publisher associated.
There are a few advantages to
having a verified publisher.
The users know the package domain
is verified, know the organization
behind it is trustworthy and can
safely use the package.
Plus, you get a sweet
icon!
If you are a person or a group of people that is interested in publishing under a single name, publishing through a verified publisher is the way to go.
You will need your own domain and prove Google that you own it. That is the domain the publisher will be associated with.
For the steps to get this done, do visit https://dart.dev/tools/pub/publishing#create-verified-publisher
So let's imagine you've completed your sweet Dart/Flutter package and you want to share it with the world!
There are some differences between Dart and Flutter packages, the main one being that the latter pertains to Flutter apps. Dart packages are more agnostic and not Flutter-specific.
If you were to use Visual Studio and
View -> Command Paletteand try to create a Flutter Package and a Dart Package, although the project structure is the same, there are two differences:pubspec.yamldoesn't load the Flutter SDK in the Dart's package and theanalysis_options.yamlfile imports different linting packages. That's it. 😄
Even if you are a publisher, currently, you can't directly publish your package to your verified publisher, you need to upload to your personal account and then transfer it to the chosen verified publisher.
In your project root, go to your terminal and type the following.
dart pub publish --dry-runThis command will make sure the package follows the pubspec format and package layout conventions. If your package yields warnings, don't worry! Just follow the suggestions (it's usually just linting or renaming files or specifying dependencies) and you should be good to go!
Once there are no issues and you feel you are ready to publish, just run the following command!
dart pub publish --dry-runYou should be prompted with the following output.
Publishing is forever; packages cannot be unpublished.
Policy details are available at https://pub.dev/policy
Do you want to publish dart_multihash 0.0.1 to https://pub.dartlang.org (y/N)?
Press y.
You should now see the following.
Pub needs your authorization to upload packages on your behalf.
In a web browser, go to https://accounts.google.com/o/oauth2/auth?axxxx
Then click "Allow access".
Waiting for your authorization...
Your link will be custom. If you visit it in your browser, it will ask you to sign-in with your Google account.
After signing-in, you should be good to go! You have now your own profile in https://pub.dev/!
If you authorize it, your terminal should display the following.
Waiting for your authorization...
Authorization received, processing...
Successfully authorized.
Uploading... (2.2s)
This means your package has been published! :tada: Congrats!
If you are part of an organization/verified publisher, you can transfer your uploaded package to it. To do so, simply head to the package page inside https://pub.dev/. You will see something similar to this.
Inside the Admin tab,
you can change ownership.
Simply select the publisher you want
and click on Transfer to Publisher.
You have to be part of the
organization/publisher to be able
to transfer ownership.
And it's as simple as that! 😊


