What is the pubspec file in Dart?

Packages are used in Dart to handle shared software like libraries and tools. These packages are published to Dart Pub and managed by Pub. Packages make code reusability and sharing easier.

In Dart, every pub package needs specific metadata to define its dependencies. This metadata is contained in a pubspec file.

A pubspec file is a YAML-coded file with the name pubspec.yaml which contains all the package metadata, fonts, and image files.

Properties of the pubspec file

The following properties can be included in a pubspec file:

name

The name property represents the package name. Dart packages must follow the naming convention for Dart identifiers such as:

  • Name must not start with a digit.
  • It should be in lowercase.
  • Each word should be separated by an underscore (_).
  • It must not be a reversed word.

For instance:

name: dropdown_menu

version

The version property represents the package version. When hosting a package on the pub.dev website, a version number is necessary, however, local-only packages do not need to include one. If we don’t include it, our package’s version is automatically set to 0.0.0.

For instance:

version: 1.0.3

description

The description property gives an explanation about the package. For instance:

description: >-
  Are you tired of creating dropdown from scratch?  
  This package can help build a dropdown and you can customize it as you wish.

repository

The repository property is optional. This represents the URL pointing to the package’s source code.

For instance:

repository: https://github.com/maria/dropdown-menu/

homepage

The homepage property is optional. It represents the URL pointing to the package’s homepage. For instance:

homepage: https://example-widget.com/dropdown

issue_tracker

The issue_tracker property is also optional and it represents the URL for the package’s issue tracker, where existing bugs can be read and new bugs can be reported. For instance:

issue_tracker: https://github.com/maria/dropdown/issues

documentation

The documentation property is also optional and it includes the URL for package’s documentation. For instance:

documentation: https://example-widget.com/dropdown-docs

dependencies

The dependencies property includes the dependencies used. It can be ignored if the package has no dependencies. For instance:

dependencies:
  bits: ^0.0.4

dev_dependencies

This contains the dev dependencies used. It can be ignored if the package has no dev dependencies. For instance:

dev_dependencies:
  flutter_test:
    sdk: flutter

environment

This includes the SDK constraint. For instance:

environment:
  sdk: '>=2.10.0 <3.0.0'

platforms

This is optional. It specifies the supported platforms on the pub.dev site. For instance:

# This package supports all platforms listed below.
platforms:
  android:
  ios:
  linux:
  macos:
  web:
  windows:

funding

The funding property is optional. This includes a list of URLs where users can support the package’s development. For instance:

funding:
 - https://www.buymeacoffee.com/my-user
 - https://www.patreon.com/my-user

false_secrets

The false_secrets property is optional. This includes the files to ignore when performing a pre-publishing search for potential leaks of secrets. For instance:

false_secrets:
 - /lib/src/hardcoded_api_key.dart
 - /test/localhost_certificates/*.pem

Note: The first time you create a new Flutter project, a simple pubspec file is generated. It is at the top of the project tree and contains information that the Dart and Flutter tooling needs to know about the project.

The pubspec.yaml file

Here is a complete pubspec.yaml file:

name: dropdown_menu
description: Are you tired of creating dropdown from scratch?
This package can help build a dropdown and you can customize it as you wish.
# The following line prevents the package from being accidentally published to
# pub.dev using `pub publish`. This is preferred for private packages.
publish_to: "none" # Remove this line if you wish to publish to pub.dev
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0
environment:
sdk: ">=2.12.0 <3.0.0"
repository: https://github.com/maria/dropdown-menu/
homepage: https://example-widget.com/dropdown
funding:
- https://www.buymeacoffee.com/my-user
- https://www.patreon.com/my-user
# This package supports all platforms listed below.
platforms:
android:
ios:
linux:
macos:
web:
windows:
false_secrets:
- /lib/src/hardcoded_api_key.dart
- /test/localhost_certificates/*.pem
dependencies:
cupertino_icons: ^1.0.2
device_info: ^2.0.2
bits: ^0.04
dev_dependencies:
flutter_test:
sdk: flutter
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
assets:
- assets/
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.
# For details regarding adding assets from package dependencies, see
# https://flutter.dev/assets-and-images/#from-packages
# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts from package dependencies,
# see https://flutter.dev/custom-fonts/#from-packages

How to add a dependency to the pubspec file

Step 1: Open the pubspec.yaml file in a Flutter project.

Step 2: Add the package to the dependencies. For instance, assuming we want to use the like_button package:

dependencies:
  like_button: ^2.0.5

Step 3: In the terminal, run flutter pub get or click Packages get in Android Studio or IntelliJ.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved