dart provides reusable package to share the code between different applications. It uses package manager(pub) tool to manage the packages.

It provides reusable libries and code for dart, flutter applications

What is a package?

Package is a reusable code, runs independently, contains classes and functions. Also contains dependencies to compile and run, shipped as a library.

Dart package manager

Dart and flutter uses native pub as a package manager By Default, dart applications loads some packages on running application.

pub is command line tool similar to npm in NodeJS applicaiton.

It used to install, uninstall, upgrade, download packages in applications

It uses two files

  • pubspec.yaml file, contains package information, third party dependencies
  • pubspec.lock file lock file that contains third party and it’s direct, indirect dependencies

pubspec.lock file:

pubspec.lock file generated after installing the package using pub install or pub get command

pubspec.yaml file:

pubspec file contains information about project metadata information, as well as other informatino

//  pubspec.yaml Example
name: packagename
description: description
version: 1.0.0
# package homepage url.
homepage: https://github.com/username/repo/README.md
# package repository link.
repository: https://github.com/Hypermona/calculator

  sdk: '>=2.18.2 <3.0.0'

   package: ^version

  package: ^version

Following are components of pubspec.yaml

  • name: package name
  • description: Short description text about package
  • version: Version as per semantic versioning(MAJOR.MINOR.PATCH)
  • homepage: package website url, contains Documentation
  • repository: Package code repository
  • environment: Contains SDK version to consider
  • dependencies: Dependencies and their version, avialiable at runtime, shipped with library.
  • dev_dependencies: Development Dependencies and their versions, avialiable at compile time, not shipped with library

Here is an command syntax.

dart pub operation
flutter pub operation

Some of the commands used by dart and flutter developers to manage the dependencies in a project.

Dart install dependency

to install depency to an applicaiton, add an dependency in dependencies of pubspec.yaml

  http: ^0.13.3

Next run, dart pub get command It reads the pubspec.yaml file and isntall the dependencies(with version) to the project. Update the pub.lock with installed version and dependent packages with version.

Before getting the dependency,

  • if pubspec.lock file exists, It get the dependency version, install the dependency.
  • if pubspec.lock file does not exists, it gets the latest version, installs it.

dart upgrade dependency to latest version

dart pub upgrade updates the latest package version in Dart project, Installs the project dependencies.. It updates the pubspec.lock with latest version.

You can update package

  • update version in pubspec.yaml, run dart pub upgrade command
  • another way, using dart pub upgrade --major-versions, It updates the pubspec.yaml and lock file with latest version

dart View outdate and latest versions

dart pub outdated command gives report which contains each package with outdated versions and latest version

PS E:\myblogs\stringutils> dart pub outdated
Showing outdated packages.
[*] indicates versions that are not the latest available.

Package Name  Current  Upgradable  Resolvable  Latest

lints         *2.1.1   *2.1.1      3.0.0       3.0.0

1 dependency is constrained to a version that is older than a resolvable version.
To update it, edit pubspec.yaml, or run `dart pub upgrade --major-versions`.
  • dart pub downgrade It downgrade the package version to version in pubspec.yaml of Dart project.

  • dart pub publish:

publish used to publish the code to pub.dev as well as github repository, So that it can be reused by other applicaitons.

  • dart pub global activate

use dart pub global activat package command. In order to activate package avialiable globally,

How to clean cache to avoid version conflicts

sometimes, if you are working with multiple versions of a same package in development, you will get version conflicts and errors.

To avoid those issues, dart pub cache clean clean cache

Flutter package manager.

pub is a default package for managing dependencies in Flutter.

  • To install dependencies
flutter pub get
  • To upgrade latest version
flutter pub upgrade