当前位置:网站首页>Dart series: using packages in dart

Dart series: using packages in dart

2022-06-24 01:25:00 Procedural stuff

brief introduction

java Use in jar Package to encapsulate useful functions , Then distribute it to maven Warehouse , For the use of others . The same in dart There is a similar concept called packages.packages Is a software package that can be shared , Can contain libraries and tools.

You can pub.dev Found in the website dart All shares in packages Information about . So why in a dart Use these in the project packages Well ?

pubspec.yaml

Simply speak , One dart Of package That is to say, it contains pubspec.yaml The catalog of .pubspec.yaml It's a description file , Used to indicate that package Meta information of , Including current package Name , Version number and dependency information .

Want to use pub.dev Upper packages, Only need pubspec.yaml Introduce the corresponding dependency .

Let's take 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 introduce two dependency packages , Namely image_picker and video_player.

get packages

When we changed pubspec.yaml after , In fact, it corresponds to package Not downloaded locally , You also need to download the corresponding... Through the following command packages:

 cd <path-to-my_app>
 dart pub get

dart pub get Will be based on pubspec.yaml Download the corresponding package according to the content configured in , And put it in the system cache .

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

Of course , You can also set PUB_CACHE To change this address .

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

After downloading the dependency package ,dart Will be in .dart_tool/ Create a package_config.json file , It is used to represent the mapping relationship between the current project and the system cache package .

# Use packages

everything , Only east wind . Now I have my bag, too , The rest is to use .

Use libary You can use keywords import. If it is dart SDK In the package , with dart: start :

 import 'dart:html';

If it's a third-party package , with package: start :

import 'package:test/test.dart';

Introduced libary It 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 Introduction part 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 introduced package is initially loaded , If some bags are very large , Or you want to load it when you use it , You can use deferred Keyword for delayed loading :

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

In use , Display call required loadLibrary() Method , To load the corresponding library:

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

Upgrade dependency

In the first run dart pub get after ,dart Will create a pubspec.lock file , Used to lock the version number of the dependent package , If it's in teamwork , This lock Files are particularly useful , It can ensure that all members of the team use the same version of the dependency package .

When you want to upgrade the corresponding dependencies , have access to dart pub upgrade command , Upgrade dependent packages .

dart pub upgrade The latest package will be generated according to the latest available package lock file .

Of course , You can also specify to upgrade a specific dependent package :

dart pub upgrade image_picker

To view the latest version of the latest dependent package , have access to :

dart pub outdated

summary

That's all dart in packages Use .

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

原网站

版权声明
本文为[Procedural stuff]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/11/20211118155010114q.html