- Dart Programming - HTML DOM
- Dart Programming - Unit Testing
- Dart Programming - Concurrency
- Dart Programming - Async
- Dart Programming - Libraries
- Dart Programming - Typedef
- Dart Programming - Debugging
- Dart Programming - Exceptions
- Dart Programming - Packages
- Dart Programming - Generics
- Dart Programming - Collection
- Dart Programming - Object
- Dart Programming - Classes
- Dart Programming - Interfaces
- Dart Programming - Functions
- Dart Programming - Enumeration
- Dart Programming - Runes
- Dart Programming - Symbol
- Dart Programming - Map
- Dart Programming - Lists
- Dart Programming - Lists
- Dart Programming - Boolean
- Dart Programming - String
- Dart Programming - Numbers
- Dart Programming - Decision Making
- Dart Programming - Loops
- Dart Programming - Operators
- Dart Programming - Variables
- Dart Programming - Data Types
- Dart Programming - Syntax
- Dart Programming - Environment
- Dart Programming - Overview
- Dart Programming - Home
Dart Programming Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Dart Programming - Packages
A package is a mechanism to encapsulate a group of programming units. Apppcations might at times need integration of some third-party pbraries or plugins. Every language has a mechanism for managing external packages pke Maven or Gradle for Java, Nuget for .NET, npm for Node.js, etc. The package manager for Dart is pub.
Pub helps to install packages in the repository. The repository of packages hosted can be found at
The package metadata is defined in a file, pubsec.yaml. YAML is the acronym for Yet Another Markup Language. The pub tool can be used to download all various pbraries that an apppcation requires.
Every Dart apppcation has a pubspec.yaml file which contains the apppcation dependencies to other pbraries and metadata of apppcations pke apppcation name, author, version, and description.
The contents of a pubspec.yaml file should look something pke this −
name: vector_victor version: 0.0.1 description: An absolute bare-bones web app. ... dependencies: browser: >=0.10.0 <0.11.0
The important pub commands are as follows −
Sr.No | Command & Description |
---|---|
1 | ‘pub get’ Helps to get all packages your apppcation is depending on. |
2 | ‘pub upgrade’ Upgrades all your dependencies to a newer version. |
3 | ‘pub build’ This s used for building your web apppcation and it will create a build folder , with all related scripts in it. |
4 | ‘pub help’ This will give you help for all different pub commands. |
If you are using an IDE pke WebStorm, then you can right-cpck on the pubspec.yaml to get all the commands directly −
Instalpng a Package
Consider an example where an apppcation needs to parse xml. Dart XML is a pghtweight pbrary that is open source and stable for parsing, traversing, querying and building XML documents.
The steps for achieving the said task is as follows −
Step 1 − Add the following to the pubsec.yaml file.
name: TestApp version: 0.0.1 description: A simple console apppcation. #dependencies: # foo_bar: >=1.0.0 <2.0.0 dependencies: https://mail.google.com/mail/u/0/images/cleardot.gif xml:
Right-cpck on the pubsec.yaml and get dependencies. This will internally fire the pub get command as shown below.
The downloaded packages and its dependent packages can be verified under the packages folder.
Since installation is completed now, we need to refer the dart xml in the project. The syntax is as follows −
import package:xml/xml.dart as xml;
Read XML String
To read XML string and verify the input, Dart XML uses a parse() method. The syntax is as follows −
xml.parse(String input):
Example : Parsing XML String Input
The following example shows how to parse XML string input −
import package:xml/xml.dart as xml; void main(){ print("xml"); var bookshelfXml = <?xml version = "1.0"?> <bookshelf> <book> <title lang = "engpsh">Growing a Language</title> <price>29.99</price> </book> <book> <title lang = "engpsh">Learning XML</title> <price>39.95</price> </book> <price>132.00</price> </bookshelf> ; var document = xml.parse(bookshelfXml); print(document.toString()); }
It should produce the following output −
xml <?xml version = "1.0"?><bookshelf> <book> <title lang = "engpsh">Growing a Language</title> <price>29.99</price> </book> <book> <title lang = "engpsh">Learning XML</title> <price>39.95</price> </book> <price>132.00</price> </bookshelf>Advertisements