🚀 DevOps Certified Professional
📅 Starting: 1st of Every Month 🤝 +91 8409492687 | 🤝 +1 (469) 756-6329 🔍 Contact@DevOpsSchool.com

How to use packages in dart

DevOps

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Packages are used in the Dart ecosystem to handle common software like libraries and tools. The pub package manager is used to obtain Dart packages. You can load packages from the local file system, remote locations, such as Git repositories, or via the pub.dev website, which lists publicly accessible packages. Pub maintains version dependencies, enabling you to obtain package versions that are compatible with one another and with the SDK version, regardless of where your packages originate.

The majority of Dart-savvy IDEs include support for utilising pub, including package creation, download, updating, and publication. You can also use the command line programme dart pub.

A pubspec file is the bare minimum that constitutes a Dart package. Some package-related metadata can be found in the pubspec. Moreover, a package can

To use a package, do the following:

  • Create a pubspec (a file named pubspec.yaml that lists package dependencies and includes other metadata, such as a version number).
  • Use dart pub get to retrieve your package’s dependencies.
  • If your Dart code depends on a library in the package, import the library.

Creating a pubspec

The top directory of your application contains a file with the name pubspec.yaml that contains the pubspec. The simplest pubspec possible merely includes the package name:

name: my_app

Here is an illustration of a pubspec that lists two packages (js and intl) that are hosted on the pub.dev website as dependencies:

name: my_app

dependencies:
  js: ^0.6.0
  intl: ^0.17.0

Run the dart pub add command to update the pubspec.yaml file without manually modifying it. The sample that follows includes a reliance on vector_math.

 $ dart pub add vector_math
Resolving dependencies... 
+ vector_math 2.1.3
Downloading vector_math 2.1.3...
Changed 1 dependency!

Getting packages

Once you have a pubspec, you can run dart pub get from the top directory of your application:

 $cd <path-to-my_app>
 $dart pub get


Importing libraries from packages

To import libraries found in packages, use the package: prefix:

import 'package:js/js.dart' as js;
import 'package:intl/intl.dart';

Everything after package: is retrieved by the Dart runtime from your app’s package_config.json file.

This style can also be used to import libraries from your own package. Let’s assume that the transmogrify package looks like this:

transmogrify/
  lib/
    transmogrify.dart
    parser.dart
  test/
    parser/
      parser_test.dart

He parser_test.dart file can import parser.dart like this:

import 'package:transmogrify/parser.dart';


Upgrading a dependency

$ dart pub upgrade

The dart pub upgrade command tells pub to regenerate the lockfile, using the newest available versions of your package’s dependencies. If you want to upgrade only one dependency, you can specify the package to upgrade:

 $dart pub upgrade transmogrify
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x