Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
In this article, we will be learning about the packages in dart. Packages in any programming language allow us to integrate pre-written codes, tools, and libraries into our current project. Using the package, we need not to write the whole code again. Instead, use the package again and again where ever needed.
The mechanism which manages the packages for programming languages is called a package manager. Some popular package managers are Maven or Gradle for Java, Nuget for .NET, pip for python, npm for Node.js, etc. Similarly, the package manager for dart is pub and it contains a file called pubspec. Let’s now discuss about pubspec file.
Pubspec File
A package in dart is a directory containing the pubspec.yaml file. YAML stands for Yet Another Markup Language and pubspec.yaml file contains some metadata about the package. A package may have dependencies, other dart libraries, resources, images, etc. You can find publically available packages here, or you can use packages from your local files or anywhere else.
Every pub package requires metadata to describe its dependencies. Pub packages shared with others must also include some more information to find them. This information is stored in the package's pubspec, a YAML file entitled pubspec.yaml.
A pubspec file contains several fields containing information about the dart's application. Some necessary fields are name, version, author, description, dependencies, etc.
Here is a simple demonstration of a pubspec.yaml file.
The above lines define the packages js and intl, along with its version.
Getting the Package
Running the command dart pub get downloads the dependency to your application, but you have to run the command at the top of your application directory.
$ dart pub get
If your app requires a published package, it will fetch it from the pub.dev website. Pub clones the Git repository for a Git-dependent package. Also included are transitive dependencies. If any package depends on the third package, the pub will also download that third package.
Let’s also discuss some of the important pub commands:
S.No.
Commands
Description
1
dart pub get
This command adds all the required packages to your project.
2
dart pub upgrade
This command upgrades the version of your dependencies.
3
dart pub build
This is used to develop your web application, and it will produce a build folder that contains all of the necessary scripts.
4
dart pub help
This command helps you with all different pub commands.
Importing the Package
If you want to use any library from the package you have downloaded in your code, you must import the library from the package.
import 'package:http/http.dart' as http;
import 'package:test/test.dart';
After importing the packages, you can use all the features provided by the package. Example: In this example, we will be importing a package and using its properties. Code:
// Importing the package to use its functionalities.
import 'package:characters/characters.dart';
void main() {
var str = 'Hi Ninja';
print('String: $str');
print('');
// Length.
print('Length: ${str.length}');
// Using the character.length property.
print('Characters.length: ${str.characters.length}');
print('');
// First character.
print('The string starts with: ${str.substring(0,1)}');
// Using the characters.first property.
print('The First character: ${str.characters.first}');
print('');
// Skip last character.
print('Substring: "${str.substring(0, str.length - 1)}"');
// Using the character.skipLast method.
print('Skipping the last character: "${str.characters.skipLast(1)}"');
print('');
// Replace characters.
var newStr = str.characters.replaceAll('Ninja'.characters, 'Great Ninja'.characters);
print('New String: "$newStr"');
}
Output:
String: Hi Ninja
Length: 8
Characters.length: 8
The string starts with: H
The First character: H
Substring: "Hi Ninj"
Skipping the last character: "Hi Ninj"
New String: "Hi Great Ninja"
Frequently Asked Questions
What is the work of a package manager?
A package manager, sometimes known as a package management system, is a collection of software tools that automate the process of installing, upgrading, customizing, and deleting computer programs on a consistent basis.
Is it possible to upgrade the package's version in our project?
Yes, we can easily upgrade the version of our packages using the dart pub upgrade command. This command upgrades all the dependencies in your project. If you want to upgrade a specific dependency, you can specify its name, e.g. dart pub upgrade name.
What is a pub tool and at what places it is used?
The pub is a Dart package manager with reusable libraries and packages for Flutter, AngularDart, and standard Dart apps.
Conclusion
This article thoroughly discussed the packages in dart and how to use them. We discussed how to declare the package in the pubspec.yaml file, then get the package using the dart pub get command and import the file to use its functions.
We hope that this blog helped you enrich your knowledge regarding packages in Dart. We can use the concepts of dart in building an android app to learn in-depth about android development. Check out our Android Development course on the Coding Ninjas website. Do upvote our blog to help other ninjas grow. Happy Coding!