Dart/Flutter - Add Package to pubspec.yaml Using Command

If you develop a project using Dart or any Dart framework including Flutter, there is a file named pubspec.yaml. The file contains the list of 3rd party libraries you've added. Dart uses that file to determine which packages to be installed. This tutorial explains the commands that you can use for installing packages including the format in the pubspec.yaml.

There are some different sources for a package. The most common is from pub.dev, which is the official repository for Dart and Flutter apps. However, you can also use a local directory, a git repository, or hosted somewhere else. In addition, each package can have some versions.

An easy way to add a package to the dependencies is by using commands started with dart pub add .... It supports all the sources above and you can also choose which version to install. If you use Flutter, you have to replace dart with flutter (e.g. flutter pub add ...). Below is the format of the command.

  dart pub add [options] [dev:]<package>[:descriptor] [[dev:]<package>[:descriptor] ...]

The examples below use foo, bar, and baz as the package name. You need to replace it with the package name you want to install.

pub.dev Package

To add a dependency hosted by pub.dev with the latest stable version, just write the name without the version.

  dart pub add foo

Result in pubspec.yaml:

  dependencies:
    foo: latest_version

It's also possible to install a specific version or use a constraint range.

  dart pub add foo:0.1.2
  dart pub add bar:^0.1.2
  dart pub add baz:'>=1.0.0 <2.0.0'

Result in pubspec.yaml:

  dependencies:
    foo: 0.1.2
    bar: ^0.1.2
    baz: ">=1.0.0 <2.0.0"

If you want to add it as a dev dependency, add dev: before the name.

  dart pub add dev:foo
  dart pub add dev:bar:^0.1.2

Result in pubspec.yaml:

  dev_dependencies:
    foo: latest_version
    bar: ^0.1.2

You can also add multiple dependencies with each separated by a space.

  dart pub add foo bar:^0.1.2

Result in pubspec.yaml:

  dependencies:
    foo: latest_version
    bar: ^0.1.2

Local Package

To install a package from a local directory, you can use the command below. The format for the descriptor is a json object containing a path field.

  dart pub add 'foo:{"path":"../local/path/to/foo"}'

Result in pubspec.yaml:

  dependencies:
    foo: ../local/path/to/foo

Git Package

To add a git as a dependency, use the command below. The descriptor must have a git field which contains the URL or path to the git. You can also add a ref field which refers to a branch or a commit and a path field which contains a location within the repository.

  dart pub add 'foo:{"git":"https://github.com/example/foo.git"}'
  dart pub add 'bar:{"git":"https://github.com/example/bar.git", "ref":"branch_name", path:"subdir"}'

Result in pubspec.yaml:

  dependencies:
    foo:
      git:
        url: https://github.com/example/foo.git
    bar:
      git:
        url: https://github.com/example/foo.git
        ref: branch_name
        path: subdir

Package from Specified SDK

If you want to add a package from a specified SDK, below is the command example.

  dart pub add 'collection:{"sdk":"flutter"}

Result in pubspec.yaml:

  dependencies:
    collection:
      sdk: flutter

Hosted Package

For package not hosted by pub.dev

. you can use the following command.

  dart pub add 'foo{"hosted":"https://example.com/foo"}'

Result in pubspec.yaml:

  dependencies:
    foo:
      hosted:
        url: https://example.com/foo

Dependency Override

To add a dependency override, add override: prefix before the name.

  dart pub add override:foo:1.0.0

Result in pubspec.yaml:

  dependency_overrides:
    foo: 1.0.0

If the package is added using pub add, it's not necessary to call pub get afterwards as using a pub add command already installs the package. If you manually edit the pubspec.yaml file, you have to run the pub get command.

Summary

In this tutorial, we have seen the commands for adding a package to the pubspec.yaml from various sources. Running the pub add command will add it to the pubspec.yaml and install it as well. In my experience, some commands are a bit difficult to use and may not work as expected, especially in Flutter. If you find it difficult to use a command or if it doesn't work, you can directly edit the pubspec.yaml file and run the pub get command. If your project uses Flutter, you have to use flutter pub ... instead of dart pub ....