Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Dart Tutorial

Dart HOME Dart Intro Dart Get Started Dart Syntax Dart Output Dart Comments Dart Variables Dart final and const Dart Data Types Dart Numbers Dart Strings Dart String Interpolation Dart String Methods Dart Booleans Dart Type Conversion Dart Null Safety Dart late Dart Operators Dart Arithmetic Dart Assignment Dart Comparison Dart Logical Dart Type Test Dart Null Operators Dart Cascade Dart If...Else Dart Switch Dart While Loop Dart For Loop Dart Break/Continue

Dart Collections

Dart Lists Dart Access List Items Dart Change List Items Dart Loop Lists Dart List Methods Dart Sets Dart Maps Dart Access Map Items Dart Loop Maps Dart Collection if/for Dart Spread Operator

Dart Functions

Dart Functions Dart Parameters Dart Named Parameters Dart Return Values Dart Arrow Functions Dart Anonymous Functions Dart Closures Dart Scope Dart Recursion

Dart Classes

Dart OOP Dart Classes/Objects Dart Constructors Dart Named Constructors Dart Factory Constructors Dart Getters and Setters Dart Static Members Dart Inheritance Dart Abstract Classes Dart Interfaces Dart Mixins Dart Extension Methods Dart Enums Dart Generics

Dart 3 Features

Dart Records Dart Patterns Dart Switch Expressions Dart Sealed Classes Dart Class Modifiers

Dart Errors

Dart Exceptions Dart Custom Exceptions

Dart Async

Dart Async Intro Dart Futures Dart async/await Dart Streams Dart Isolates

Dart Libraries

Dart Imports Dart Packages Dart Math Dart Dates Dart JSON Dart RegExp Dart User Input Dart Files Dart Testing

Dart Examples

Dart Examples Dart Exercises


Dart Packages


Share

Dart Packages

A package is a collection of Dart code that someone has shared, so that other people can use it.

Packages save you time, because you do not have to write everything yourself.

Most Dart packages are on pub.dev, the official package site for Dart and Flutter.

The tool that gets packages for you is called pub, and you use it with dart pub.

Note: To use packages, your code must be in a Dart project.

You learned how to create a project with dart create in the Get Started chapter.


The pubspec.yaml File

Every Dart project has a file called pubspec.yaml.

It has the name of the project, the Dart version it needs, and the packages it uses.

This is the file that dart create my_app makes:

pubspec.yaml

name: my_app
description: A sample command-line application.
version: 1.0.0

environment:
  sdk: ^3.14.1

dependencies:
  path: ^1.9.0

dev_dependencies:
  lints: ^6.0.0
  test: ^1.25.6
  • dependencies are packages that your program uses when it runs
  • dev_dependencies are packages that you only use while you write the code, like the test package

The ^ in ^1.9.0 means "version 1.9.0 or a newer version that works the same way".



Add a Package

The intl package formats dates and numbers.

Open a terminal in the project folder, and add the package with dart pub add:

Example

dart pub add intl

Result:

Resolving dependencies...
Downloading packages...
+ clock 1.1.3
+ intl 0.20.3
Changed 2 dependencies!

Pub downloaded intl, and also clock, because intl needs it.

Pub also added the package to pubspec.yaml:

pubspec.yaml

dependencies:
  intl: ^0.20.3
  path: ^1.9.0

Your version numbers can be newer.


Use a Package

To use a package, import it with package:, the package name and the file name:

Example

bin/my_app.dart

import 'package:intl/intl.dart';

void main() {
  var date = DateTime(2026, 3, 14);
  print(DateFormat('d MMMM yyyy').format(date));
  print(DateFormat('EEEE').format(date));

  var price = NumberFormat('#,##0.00');
  print(price.format(1234567.891));
}

Run the project with dart run:

Result:

14 March 2026
Saturday
1,234,567.89

DateFormat and NumberFormat come from the intl package.

The Imports chapter explains import in more detail.

Note: Packages from pub.dev do not work in our "Try it Yourself" editor.

Run the examples on this page in a project on your own computer.


Other pub Commands

CommandWhat it does
dart pub add nameAdds a package to dependencies and downloads it
dart pub add dev:nameAdds a package to dev_dependencies
dart pub remove nameRemoves a package from the project
dart pub getDownloads all the packages in pubspec.yaml
dart pub upgradeUpdates the packages to the newest versions that pubspec.yaml allows
dart pub outdatedShows packages that have newer versions

For example, this removes the path package:

Example

dart pub remove path

Result:

Resolving dependencies...
Downloading packages...
  path 1.9.1 (from direct dependency to transitive dependency)
Changed 1 dependency!

The path package is still downloaded, because another package that the project uses needs it.

But it is no longer a dependency of your project, so your own code should not import it.


Choose a Good Package

Anyone can share a package on pub.dev, so check a package before you use it.

On the package page on pub.dev, look at:

  • Likes and downloads: how many people use the package
  • Pub points: a score for code quality and documentation
  • Publisher: a verified publisher, like dart.dev or google.dev, shows who made the package
  • Last updated: a package that is updated often is usually safer


Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.