• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

【Dart学习】package_manage Pub

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

A package is a mechanism to encapsulate a group of programming units. Applications might at times need integration of some third-party libraries or plugins. Every language has a mechanism for managing external packages like 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 https://pub.dartlang.org/.

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 libraries that an application requires.

Every Dart application has a pubspec.yaml file which contains the application dependencies to other libraries and metadata of applications like application name, author, version, and description.

The contents of a pubspec.yaml file should look something like 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 application is depending on.

2

‘pub upgrade’

Upgrades all your dependencies to a newer version.

3

‘pub build’

This s used for building your web application 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 like WebStorm, then you can right-click on the pubspec.yaml to get all the commands directly −

Installing a Package

Consider an example where 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.

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 application. 
#dependencies: 
#  foo_bar: '>=1.0.0 <2.0.0' 
dependencies: https://mail.google.com/mail/u/0/images/cleardot.gif
xml: 

Right-click 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 = "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>'''; 
   
   var document = xml.parse(bookshelfXml); 
   print(document.toString()); 
}

It should produce the following output −

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> 

转载:https://www.tutorialspoint.com/dart_programming/dart_programming_packages.htm


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ChromeDevEditor:一个新的JavaScript和DartIDE发布时间:2022-07-13
下一篇:
一、Dart Intellij IDEA 下安装 Dart 插件发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap