All things you need to know about pubspec.yaml file

Pravin Palkonda
6 min readJan 11, 2023

--

When we create a new flutter application, it creates the predefined directories and files. The pubspec.yaml file is one of those predefined files. While working on the application we need to add external libraries to use the features provided by the library.

But where do we add these dependencies?

It is pubspec.yaml file, where we add the dependencies. It is the file that we mainly use for two purposes, one is to add dependencies and another is to add assets to the application ( assets means images, fonts, etc ).

The pubspec.yaml file is case-sensitive as well as space sensitive. So it is necessary to follow proper indentation while adding dependencies or assets. In this file, all the data are available in the key-value pair.

This file also contain information which is commented. It is good to read the comments properly which will help to understand the file properly.

name: flutter_demo
description: A new Flutter project.

# The following line prevents the package from being accidentally published to
# pub.dev using `flutter 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 is used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 1.0.0+1

environment:
sdk: '>=2.18.5 <3.0.0'

# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions
# consider running `flutter pub upgrade --major-versions`. Alternatively,
# dependencies can be manually updated by changing the version numbers below to
# the latest version available on pub.dev. To see which dependencies have newer
# versions available, run `flutter pub outdated`.
dependencies:
flutter:
sdk: flutter
sqflite: ^2.2.2



# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2

dev_dependencies:
flutter_test:
sdk: flutter

# The "flutter_lints" package below contains a set of recommended lints to
# encourage good coding practices. The lint set provided by the package is
# activated in the `analysis_options.yaml` file located at the root of your
# package. See that file for information about deactivating specific lint
# rules and activating additional ones.
flutter_lints: ^2.0.0

# 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 packages.
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:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg

# 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

Packages related information

The file contains the name of the package as well as description of the package. Here we can edit the description field to add the short description of the package.

name and description.

Environment related information

The file also contains the version and environment property. Here the version is defined as the version of the package.

version: 1.0.0+1
/// here 1.0.0 is the version munber
/// and after the + symbole is a build number

Version is necessary, when we upload the application to play store or apple store. So when we upload the new version of the app , the version property in the pubspec.yaml should be increased.

version: 1.0.0+1
/// here 1.0.1 is the updated version

It also contains environment property, which defined the dart SDK version currently being used in the project.

application version and dart SDK version

All about dependencies

The dependencies which we need to add are added in dependencies or dev dependencies section.

The dependencies which are added in the dependencies section are considered as those libraries will be added for deployment purposes. The dependencies which are added in dev_dependecies are considered for development purposes.

dependencies:
flutter:
sdk: flutter
/// here using sqfite dependency with version 2.2.2
sqflite: ^2.2.2


cupertino_icons: ^1.0.2

dev_dependencies:
flutter_test:
sdk: flutter
dependencies

It also has a property flutter lints. Which recommend us to write good coding practices.

We can also edit the lints which we need to remove or add, in analys_options.yaml file. Again it is a yaml file where we can do the changes regarding lints.

Flutter specific changes

The pubspec.yaml file also has a flutter property which is the main property related to specific flutter package. In this flutter property we can also add assets or fonts to the application.

This file contains the uses-material-design property which is always a true value. This property ensures that we are using material icons or fonts in the package.

Assets

To add an assets to the application, we must provide path in the pubspec.yaml file.

Just create an assets directory and add an image in that directory. Uncomment the asset section code to use the predefined code and follow the indentation. Here we can add multiple image paths.

# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg

After removing comments

/// here we need to add a path to images
/// consider images are in a assets folder that we have created

assets:
- assets/image_name.image_extension
- assets/iamge_1.jpeg

Fonts

To add fonts to the application we have a property fonts in pubspec.yaml file. In this section we need to define the font family name and provide a path fonts file that we have added.

We can add multiple fonts to the application, just their family name should be different.

fonts:
- family: Schyler /// here we provide font family name
fonts:
- asset: fonts/Schyler-Regular.ttf /// here we provide file path
- asset: fonts/Schyler-Italic.ttf
style: italic /// here we defined the style

Note : Here is main important thing, that whenever the pubspec.yaml file is changed we need to fire the flutter pub get command in the terminal or click the Get packages button which is available in the pubspec.yaml file on the top right side of editor. By doint this, it will cache all the files properly.

So final conclusion is, pubspec.yaml file contains all the metadata of the application. It has a dart and flutter SDK version and we can add external libraries to use in the application.

Let me know in the comments if I need to correct anything. I will try to improve it.

Clap if you like the article. 👏

--

--