Introduction

The jar package is used in java to encapsulate useful functions, and then distributed to the maven warehouse for other people to use. Similarly, there is a similar concept in dart called packages. Packages are packages that can be used to share, and can include libraries and tools.

You can find information about all shared packages in dart on the pub.dev website. So how do you use these packages in a dart project?

pubspec.yaml

To put it simply, a dart package is a directory containing pubspec.yaml. pubspec.yaml is a description file used to indicate the meta information of the package, including the name, version number, and dependency information of the current package.

To use the packages on pub.dev, you only need to introduce the corresponding dependencies in pubspec.yaml.

Let's give an example:

name: app2
description: a demo app
version: 1.0.0+1

environment:
  sdk: ">=2.7.0 <3.0.0"

dependencies:
  image_picker: ^0.6.7+22
  video_player: ^0.10.12+5

Here we have introduced two dependency packages, image_picker and video_player.

get packages

When we modify the pubspec.yaml, the corresponding package is not downloaded locally. You also need to download the corresponding packages through the following command:

 cd <path-to-my_app>
 dart pub get

Dart pub get will download the corresponding package according to the content configured in pubspec.yaml and place it in the system cache.

In Mac or Linux systems, the address of this cache directory is: ~/.pub-cache, in windows the address of this directory is: %LOCALAPPDATA%\Pub\Cache.

Of course, you can also change this address by setting PUB_CACHE.

If the package you depend on depends on other packages, the other dependent packages will also be downloaded.

After downloading the dependency package, dart will create a package_config.json file in the .dart_tool/ directory to indicate the mapping relationship between the current project and the system cache package.

# Use packages

All is ready except for the opportunity. Now the package is also available, and the rest is used.

The keyword import can be used with libary. If it is a package in the dart SDK, start with dart::

 import 'dart:html';

If it is a third-party package, start with package::

import 'package:test/test.dart';

The introduced libary can also be renamed:

import 'package:lib1/lib1.dart';
import 'package:lib2/lib2.dart' as lib2;

// Uses Element from lib1.
Element element1 = Element();

// Uses Element from lib2.
lib2.Element element2 = lib2.Element();

You can also use show and hide to introduce part of the library:

// Import only foo.
import 'package:lib1/lib1.dart' show foo;

// Import all names EXCEPT foo.
import 'package:lib2/lib2.dart' hide foo;

By default, the imported packages are initially loaded. If some packages are very large, or you want to load them when you use them, you can use the deferred keyword for delayed loading:

import 'package:greetings/hello.dart' deferred as hello;

When in use, you need to explicitly call the loadLibrary() method to load the corresponding library:

Future<void> greet() async {
  await hello.loadLibrary();
  hello.printGreeting();
}

Upgrade dependency

After running dart pub get for the first time, dart will create a pubspec.lock file to lock the version number of the dependent package. This lock file is particularly useful if it is in teamwork, as it can guarantee all members of the team All dependent packages of the same version are used.

When you want to upgrade the corresponding dependency, you can use the dart pub upgrade command to upgrade the dependency package.

dart pub upgrade will generate the latest lock file based on the latest available package.

Of course, you can also specify to upgrade a specific dependency package:

dart pub upgrade image_picker

To view the latest version of the latest dependency package, you can use:

dart pub outdated

Summarize

The above is the use of packages in dart.

This article has been included in http://www.flydean.com/09-dart-packages/

The most popular interpretation, the most profound dry goods, the most concise tutorial, and many tips you don't know are waiting for you to discover!

Welcome to pay attention to my official account: "Program those things", know technology, know you better!


flydean
890 声望433 粉丝

欢迎访问我的个人网站:www.flydean.com