Dart Packages
Every programming language has some in-built functions stored inside the header files that makes the task much easier for the programmer. The Dart Packages refer to the compilation of a well-organized, independent, and reusable code unit. Applications may be required to use third-party libraries or packages. Dart offers a comprehensive set of automated packages that automatically load when the dart console launches. However, if we need packages other than the default packages they need to be packaged and loaded explicitly in order to use them. Once the package is loaded, it can be used throughout the Dart program.
Dart Package Manager
Dart has a built-in package manager, called a pub. It is primarily used for organizing, managing third-party libraries, tools, dependents, and is used to store packages in storage. The entire Dart application contains a ‘pubspec.yaml’ file that includes the metadata of the file. Package metadata contains the details about the author, version, application name, and description. The complete full form of ‘yaml’ is ‘Yet Another Markup Language’. ‘pubspec.yaml’ is used to download various libraries required by the application during the program. The ‘pubspec.yaml’ file should look like this.
name : 'vector_victor'
version : 0.0.1
description : An absolute bare-bones web app.
...
dependencies : browser : ' >= 0.10.0 < 0.11.0 '
Dart IDE provides built-in pub support that includes creating, downloading, updating, and publishing packages, alternatively we can use the pub command line. The following is a list of a few important pub commands.
Sr.
No |
Commands |
Description |
1. |
pub get |
This command is used to retrieve all the packages
of application on which yaml is dependent on. |
2. |
pub upgrade |
This command is used to upgrade all application
dependencies to the modern version. |
3. |
pub build |
This command is used to construct the web
application. It does so by creating a build folder, with all related scripts
in it. |
4. |
pub help |
This command provides all the solutions to the
queries while using the Dart packages. |
Let us dive into how to install a package :
Installing a Package
Follow the listed steps to install and use the packages in the Dart application :
Step - 1: Write the name of the package in the dependencies section of ‘pubspec.yaml’ file of the project. Then run the command written below to find the package installed in the project.
pub get
The above command will download the package under the packages folder in the application directory.
Example -
name : TestApp version : 0.0.1 description : A simple dart application dependencies : xml :
We have successfully added the xml to the project dependencies. Now the Dart XML packages are ready to be used in the project by importing it. It can be imported as follows.
import 'package:xml/xml.dart' as xml;
Read XML String
We can read XML string and authenticate the input; Dart XML provides a parse() method to read the string input. The syntax is given below.
xml. parse(String input) :
Let's have a look at the following example:
Example - Parsing XML String Input
In the following example, we display the parsing XML string input.
import 'package:xml/xml.dart' as xml ; void main( ) { print( " Parsing XML " ) ; var bookstoreXml = ''' < ? xml version = " 1.0 " ? > <bookstore> <book> <title lang = "English"> Stop Worrying and Start Living </title> <price> 350.00 </price> </book> <book> <title lang = "English"> How to win friends and influence people </title> <price> 150.00 </price> </book> <price> 400.00 </price> </bookstore> ''' ; var document = xml.parse( bookstoreXml ) ; print( document.toString( ) ) ; }
Output:
Parsing XML 350.00 150.00 400.00
Supported fields in YAML Language : There are some fields that we mention in the ‘yaml’ file. Some of them are as follows :
- name : Required for all packages.
- version : Required for packages hosted on the pub.dev site.
- description : Required for packages hosted on the pub.dev site.
- homepage : A URL that identifies the home page of a package (or source code repository).
- repository : URL that identifies the source code of the package.
- issue_tracker : A URL that identifies a package tracker problem.
- documentation : URL pointing to package documents.
- dependencies : It can be left out if your package does not depend on dependency.
- dev_dependencies : Can be left out if your package does not have dev dependencies.
- dependency_overrides : Can be omitted if you do not need to write over any dependencies.
- environment : Required from Dart 2.
- executables : Used to apply usable package to your PATH.
- publish_to : Specify where to publish the package.
- false_secrets : Specify files you may disregard when conducting pre-publish searches for possible leaks of secrets.