How to use packages

    Most offer support for using pub thatincludes creating, downloading, updating, and publishing packages.Or you can use on the command line.

    At a minimum,a Dart package is a directory containing a .The pubspec contains some metadata about the package. Additionally,a package can contain dependencies (listed in the pubspec),Dart libraries, apps, resources, tests, images, and examples.

    To use a package, do the following:

    • Create a pubspec (a file named pubspec.yaml that lists package dependencies and includesother metadata, such as a version number).
    • Use pub to get your package’s dependencies.
    • If your Dart code depends on a library in the package, import the library.

    The pubspec is a file named pubspec.yamlthat’s in the top directory of your application.The simplest possible pubspec lists only the package name:

    Here is an example of a pubspec that declares dependencies ontwo packages (js and intl) that are hosted on the pub.dev site:

    1. name: my_app
    2. dependencies:
    3. js: ^0.6.0

    For details on creating a pubspec,see the pubspec documentationand the documentation for the packages that you want to use.

    Once you have a pubspec, you can run pubget from the top directory of your application:

    1. $ cd <path-to-my_app>
    2. $ pub get

    This process is called getting the dependencies.

    Pub creates a.packages file (under your app’s top directory)that maps each package namethat your app depends on to the corresponding package in the system cache.

    To import libraries found in packages, use thepackage: prefix:

    The Dart runtime takes everything after package:and looks it up within the .packages file foryour app.

    You can also use this style to import libraries from within your own package.Consider the following pubspec file, which declares a dependency onthe (fictional) transmogrify package:

    1. name: my_app
    2. dependencies:
    3. transmogrify:

    Let’s say that your package is laid out as follows:

    1. transmogrify/
    2. lib/
    3. transmogrify.dart
    4. parser.dart
    5. test/
    6. parser/
    7. parser_test.dart

    The parsertest file _could import parser.dart like this:

    But that’s a fragile relative path. If parser_test.dart ever movesup or down a directory, that path breaks.Instead, you can do as follows:

    1. import 'package:transmogrify/parser.dart';

    This way, the import can always get to parser.dart regardless of where theimporting file is.

    If your package is an application package,you should check this file into.That way, everyone working on your app uses the same versionsof all of the packages.Checking in the lockfile also ensures that your deployed appuses the same versions of code.

    When you’re ready to upgrade your dependencies to the latest versions,use pub upgrade:

    1. $ pub upgrade

    That command tells pub to regenerate the lockfile, using the newestavailable versions of your package’s dependencies.If you want to upgrade only one dependency,you can specify the package to upgrade:

    That command upgrades to the latest versionbut leaves everything else the same.

    The following pages have more information about packages andthe pub package manager.

    The pub tool provides the following commands:

    For an overview of all the pub commands,see the pub tool documentation.

    gives solutions to problems thatyou might encounter when using pub.