在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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 −
The important pub commands are as follows −
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 PackageConsider 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.
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 StringTo 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 InputThe following example shows how to parse XML string input −
It should produce the following output −
转载:https://www.tutorialspoint.com/dart_programming/dart_programming_packages.htm |
请发表评论