Dart Packages
A dart package is a standalone, reusable code item that is arranged neatly. Applications may be required in order to use third-party packages or libraries. The compiled software, sample data, and a set of classes, functions, or units of code for certain tasks are typically included in the package. When the Dart console launches, a large number of default packages are loaded automatically by Dart. However, in order to use them, we must install and load them explicitly if we require packages different than the ones that come in the package bundle. A package can be utilized throughout the entire Dart environment once it has been loaded.
Dart Package Manager
The ability to handle external packages, like Nuget for.NET, Gradle or Maven for Java, npm for Node.js, etc., is built into every language. A pub is the name for the built-in package manager in Dart. Its main functions include managing dependencies, organizing third-party libraries and tools, and installing packages from the repository. Each Dart application has a pubspec.yaml file that contains the file’s metadata. The package’s metadata contains the application name, description, author, and version. Yaml stands for Yet Another Markup Language in its entire form. The different libraries that an application need when programming are downloaded via the pubspec.yaml file. The following is how the pubspec.yaml file should appear.
name: 'vector_victor'
version: 0.0.1
description: An absolute bare-bones web app.
...
dependencies: browser: '>=0.10.0 <0.11.0'
If not, we can use the pub command line. The Dart IDEs come with built-in support for using the pub, which includes package creation, download, updating, and publishing. The list of a few key pub commands is as follows.
Sr. No | Description |
---|---|
pub get | It is used to get all packages of application dependent on. |
pub upgrade | It is used to upgrade all application dependencies to the modern version. |
pub build | It is used to construct your web application, and it will create a build folder, with all related scripts in it. |
pub help | It is used to get help related to all pub commands or when we stuck while programming. |
Installing a Package
The installation of the package in the project is defined by the steps that follow.
Step 1: In the project’s pubspec.yaml file, write the package name in the dependencies section. To find the package installed in the project, do the following command.
pub get
Example
name: TestApp
version: 0.0.1
description: A simple dart application
dependencies:
xml:
The xml has been added to the project dependencies. By importing it, we can now use the Dart XML package in the project. This is how it can be imported.
import 'package:xml/xml.dart' as xml;
Read XML String
With the help of Dart XML’s parse() function, we can read an XML string and verify the input. Below is the syntax.
xml. parse(String input):
Example
import 'package:xml/xml.dart' as xml;
void main(){
print("xml");
var bookstoreXml = '''
Who will cry when you die
150.00
The Alchemist
90.00
200.00
''';
var document = xml.parse(bookstoreXml);
print(document.toString());
}
Output
xml
150.00
90.00
200.00