当前位置:网站首页>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/
边栏推荐
- Grab startup logcat
- 用一个软件纪念自己故去的母亲,这或许才是程序员最大的浪漫吧
- SAP executes PGI on the delivery order of STO and reports an error -fld selectn for Mvmt type 643 acct 400020 differences
- 跨域和JSONP
- How to realize court face recognition / flow statistics based on easycvr technology?
- PVE enables the hardware graphics card pass through function
- Disaster recovery series (VI) -- data storage disaster recovery construction
- [new secsha!] Have you got the 22 year new seckill activity of Tencent cloud?
- Real time preview of RTSP video based on webrtc
- Golang gets the start timestamp and end timestamp of a past or future week or month
猜你喜欢

【Flutter】如何使用Flutter包和插件

Zhongshanshan: engineers after being blasted will take off | ONEFLOW u

Data management: business data cleaning and implementation scheme

【Redis进阶之ZipList】如果再有人问你什么是压缩列表?请把这篇文章直接甩给他。

Shardingsphere-proxy-5.0.0 implementation of capacity range partition (V)

WinSCP和PuTTY的安装和使用

ShardingSphere-proxy-5.0.0容量范围分片的实现(五)

Cvpr2022 𞓜 thin domain adaptation

Use recursion to form a multi-level directory tree structure, with possibly the most detailed notes of the whole network.

LSF opens job idle information to view the CPU time/elapse time usage of the job
随机推荐
Salesforce batch apex batch processing (I)
什么是养老理财?养老理财产品有哪些?
[technology planting grass] talk about the system design and architecture of large-scale shopping platform
Remove the cloud disk service display "continued" logo
Dart series part: asynchronous programming in dart
【Flutter】如何使用Flutter包和插件
Echo framework: automatically add requestid
PVE enables the hardware graphics card pass through function
How does the fixed assets management software reduce costs and increase efficiency for enterprises?
The best Base64 encoding and decoding tutorial in the whole network, with 6 examples!
Thread safety and lock optimization
js输入输出语句,变量
对抗训练理论分析:自适应步长快速对抗训练
How to build high quality and high brand websites what should be paid attention to in the construction of enterprise websites
[shutter] how to use shutter packages and plug-ins
13 `bs_duixiang.tag标签`得到一个tag对象
Feasibility of importing UE4 using gltf with instances
Everything I see is the category of my precise positioning! Open source of a new method for saliency map visualization
CTF steganography
On November 11, 2021, live broadcast e-commerce opened a new way to play