What is pubspec.yaml file in the flutter project? (Class 26)

pubspec.yaml

What is pubspec.yaml file in the Flutter Project?

Every project of Flutter has pubspec.yaml file containing the configuration of the project. We learned the YAML structure in the last class. Our main objective of learning the YAML structure was to understand pubscpec.yaml to work on it. We can easily read the structure of pubspec.yaml after learning YAML which is not very complex.

 

Where the pubspec.yaml file exists

The pubspec.yaml file exists in the Project showing at the top left bar of Flutter. We can open it by double-clicking pubspec.yaml file and it will come as a new tab in Flutter.

 

Parents & siblings fields

There are different parents & siblings fields in pubspec.yaml file. If two or more fields have the same indent, these are called siblings such as the following ‘name’ and ‘description’ fields:

name: hello_world
description: A new Flutter project.

 

In case there is a space before the second field, it is called a child field. As ‘sdk’ is a child field of ‘environment’ field in the following example:

environment:
  sdk: ‘>=3.0.6 <4.0.0’

 

Similarly, there can be multiple siblings and child/ parent fields.

 

Types of fields in pubspec.yaml:

There are two types of fields in pubspec.yaml file:

 

Required fields:

As clear from its name, we cannot miss the required fields in pubspec.yaml file. These are mandatory and play an important role in the configuration of a project. For example, the very first field of ‘name’ in pubspec.yaml file is a required field:

name: new_course_flutter

 

 

Optional fields:

Optional fields are created to help and guide us. We have the liberty to skip optional fields without any issue. For example ‘description’ is an optional field:

description: A new Flutter project.

 

Package

When we create a project in Flutter it is called a package. Every package must have pubspec.yaml file.

 

Types of Packages

There are following two types of packages:

 

Application Package

When we create a project and set its type as an application, it is called an application package.

 

Library Package

When we create a project and set its type as a plugin, it is called a Library Package. Plugins are used to enhance functionality and for additional features.  We will use Flutter Plugins in coming classes from the following site:

pub.dev

 

Referencing

As we learned in previous classes we can create multiple dart files in the Lib folder depending on our need. When we create a file in the lib folder, we have to give a reference of that file in our main file too for its proper functioning. For example, we created a new dart file login and made a function in it like this:

login() {
  var a = 30, b = 40;
  print(a + b);
}

 

Now we need to give its reference while using it in other files or main.dart file of Flutter like this:

import ‘package:hello_world/login.dart’;

 

In the above example, Hello World is our main file whereas login.dart is a newly created file. We have to follow the same pattern for referencing while creating all new files.

 

Is Flutter a Package and also needs a reference?

Yes, Flutter is also a package and it is categorized as a main or core package. When we create a new Flutter Project we give its reference on top as under:

import ‘package:flutter/material.dart’;

 

Pub Get in pubspec.yaml

After making any changes in pubspec.yaml file we need to click on Pub Get on the top bar for smooth functioning.

 

ROBINA KOUSAR
Content Writer
**************************
You can get all Mobile App Development Classes link from here:-

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.