Dart Packages

 

Dart Packages

Dart package is the collection of a well-organized, independent, and reusable code unit. Applications might be needed to implement third-party libraries or packages. The package generally contains a set of classes, functions, or unit of code for specific tasks along with the compiled program and sample data. Dart provides an extensive set of default packages that load automatically when the dart console starts. However, if we need packages other than the default packages then its need to installed and loaded explicitly in order to use. When a package is loaded, it can be used in the whole Dart environment.

Dart Package Manager

Every language provides the functionality for handling the external packages such as Nuget for .NET, Gradle or Maven for Java, npm for Node.js, etc. Dart has the inbuilt package manager, which is called a pub. It is basically used to organize, manage the third-party libraries, tools, dependencies, and also used to install the packages in the repository. Every Dart application consists of a pubspec.yaml file which includes the metadata of the file. The metadata of the package holds the author, version, application name, and description. The full form of the yaml is a Yet Another Markup Language. The pubspec.yaml is used to download the various libraries that application needs during programming. The pubspec.yaml file must look like as follows.

  1. name: 'vector_victor'   
  2. version: 0.0.1   
  3. description: An absolute bare-bones web app.   
  4. ...   
  5. dependencies: browser: '>=0.10.0 <0.11.0'   

The Dart IDE's provides the inbuilt support for using the pub that consist of creating, downloading, updating, and publishing packages, Otherwise we can use the pub command line. The following is the list of the few important pub commands.

Sr. NoDescription
pub getIt is used to get all packages of application dependent on.
pub upgradeIt is used to upgrade all application dependencies to the modern version.
pub buildIt is used to construct your web application, and it will create a build folder, with all related scripts in it.
pub helpIt is used to get help related to all pub commands or when we stuck while programming.

Installing a Package

The following steps are defining the installation of the package in the project.

Step - 1: Write the package name in the dependencies section of project's pubspec.yaml file. Then run the below command to find the package installed in project.

  1. pub get   

The above command will download the package under the packages folder in the application directory.

Example -

  1. name: TestApp  
  2. version: 0.0.1  
  3. description: A simple dart application  
  4. dependencies:  
  5. xml:  

We have added the xml to the project dependencies. Now we can use Dart XML package in the project by importing it. It can be imported as follows.

  1. 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.

  1. 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.

  1. import 'package:xml/xml.dart' as xml;   
  2. void main(){   
  3.    print("xml");   
  4.    var bookstoreXml = '''<?xml version = "1.0"?>   
  5.    <bookstore>   
  6.       <book>   
  7.          <title lang = "English">Who will cry when you die </title>   
  8.          <price>150.00</price>   
  9.       </book>   
  10.         
  11.       <book>   
  12.          <title lang = "English">The Alchemist </title>   
  13.          <price>90.00</price>   
  14.       </book>   
  15.       <price>200.00</price>   
  16.    </bookstore>''';   
  17.      
  18.    var document = xml.parse(bookstoreXml);   
  19.    print(document.toString());   
  20. }  

Output:

xml 
 
    
       
      150.00 
    

    
       
      90.00 
    
   200.00 

Comments