Dart – Basics of Packages

The package is a set of dart program organized in an independent, reusable unit. It contains a set of functions and classes for a specific purpose or utility along with the compiled code and sample data. Dart comes with a rich set of default packages, loaded automatically when Dart console is started. Any other package other than the default needs to be installed and loaded explicitly first in order to use it. Once a packages Is loaded, it can be used throughout the Dart environment.

Dart Package Manager

Dart comes with an inbuilt Package Manager known as pub package manager. It is used to Install, organize, and manage third-party libraries, tools, and dependencies. Every Dart application has a pubspec.yaml file that keeps track of all the third-party libraries and application dependencies along with the metadata of applications like application name, author, version, and description, Most of the Dart IDEs offer in-built support for using a pub that includes creating, downloading, updating, and publishing packages. Otherwise, pub ca be used on the command line. Below is a list of the important pub commands

list of the important pub commands

Installing a Package

Step 1: Add the package name in the dependencies section of your project’s pubspec.yaml file. Then, the following command can be run from the application directory to get the package installed in the project.

pub get

This will download the packages under the packages folder in the application directory.



Example:  

An application needs to parse XML. Dart XML is a lightweight library that is open source and stable for parsing, traversing, querying, and building XML documents.

Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

name: GFGapp
version: 0.0.1 
description: A simple core application. 
#dependencies: 
#  foo_bar: '>=1.0.0 <2.0.0' 
dependencies: https://mail.google.com/mail/u/0/images/cleardot.gif
xml:

chevron_right


To refer to the dart XML in the project. The syntax is as follows:

import 'package:xml/xml.dart' as xml;

Read XML String

XML string can read and verify the input, Dart XML uses a parse() method.

Syntax: xml.parse(String input):

Example: 

Parsing XML String Input. The following example shows how to parse XML string input.

Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

// Dart program to illustrate 
// parsing XML in Dart
import 'package:xml/xml.dart' as xml; 
void main(){ 
   print("GFG-XML"); 
   var bookshelfXml = '''

chevron_right


Output:

GFG-XML
<?xml version = "1.0"?><bookshelf>  
  <book>  
     <title lang = "english">Growing a Language</title>  
     <price>29.99</price>  
  </book>  

  <book>  
     <title lang = "english">Learning XML</title>  
     <price>39.95</price>  
  </book>  
  <price>132.00</price>  
</bookshelf> 

My Personal Notes arrow_drop_up

Stepping steps of my ownself

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.