Skip to content

Adding Packages

When adding packages to your Dart projects, there are two ways in which these packages can be referenced inside your pubspec.yaml.

Adding a package from pub.dev

To add a package from pub.dev πŸ”—, you can simply add the package name to the dependencies section of your pubspec.yaml file.

dependencies:
flutter:
sdk: flutter
vyuh_core: ^1.0.0

For a list of all the packages which are publicly available on pub.dev, refer to the article on packages.

Adding a package from the Enterprise Plan Package Registry

Adding a package from the private pub registry requires you to set up a token for the hosted provider. You will get this token during the onboarding process for the enterprise plan. Once you have this token, you can add the hosted provider as below.

  1. Keep the details handy

    Keep the details of the hosted provider handy. You will need the URL of the hosted provider and the token to authenticate the requests. Let’s capture these with the following variables for rest of the steps:

    • <token>: secret token to authenticate requests to the hosted provider
    • <private-pub-registry>: URL of the hosted provider
  2. Add the hosted provider

    Use the <token> given to you during the onboarding stage and enter that as your secret token.

    Terminal window
    dart pub token add https://<private-pub-registry>
    Enter secret token: <Type token on stdin>
    Requests to "https://<private-pub-registry>" will now be
    authenticated using the secret token.
  3. Add the package to your pubspec.yaml

    You can now add the package from the hosted provider like so:

    Terminal window
    dart pub add --hosted-url=https://<private-pub-registry> <package-name>

    An easier way is to just copy this snippet for each enterprise package and paste into your pubspec.yaml:

    <package-name>:
    hosted: https://<private-pub-registry>
    version: ^1.0.0
  4. Bootstrap with Melos

    Now when you run melos bootstrap inside your project, the package will be fetched from the hosted provider.

Summary

This guide covers how to add packages from both the public and private package registries. The private package registry requires you to set up a token for the hosted provider. Once done you can add the package to your pubspec.yaml and run melos bootstrap to fetch the package.