当前位置:网站首页>Dart series: creating a library package
Dart series: creating a library package
2022-06-24 01:12:00 【Procedural stuff】
brief introduction
stay dart In the system , Yes pubspec.yaml The application of files can be called package. and Libray package It's a special kind of package, This package can be relied on by other projects . This is what is commonly called a library .
If you want what you wrote dart The program can be uploaded to pub.dev On , Or provide it to others , Let's take a look at this article .
Library package Structure
Let's take a look at library package Structure :
app3 ├── lib │ └── main.dart └── pubspce.yaml
This is the simplest Library package Structure , stay root Below directory , We have a pubspce.yaml file . And then there's another one lib The contents are library Code for .
Generally speaking lib The following libraries can be referenced externally . If it is library Internal documents , You can put lib/src Below directory , The file here means private Of , It should not be introduced by other programs .
If you want to src The package in is exported for external use , Can be in lib Below dart Use... In the document export, What will be needed lib export . So other users only need import This is just a file .
export An example of this is :
library animation; export 'src/animation/animation.dart'; export 'src/animation/animation_controller.dart'; export 'src/animation/animations.dart'; export 'src/animation/curves.dart'; export 'src/animation/listener_helpers.dart'; export 'src/animation/tween.dart'; export 'src/animation/tween_sequence.dart';
The code above is flutter Of animation library .
Import library
How to use it ? We can use import Statement to import the corresponding lib:
import 'package:flutter/animation.dart';
If it is the import of internal files , Relative paths can be used . Only when importing external package You only need to add package: Prefix .
Conditional import and export library
because dart It is designed to work on different platforms , So a library You may need to import or export different files on different platforms library file , This is called conditional import and export .
For example, you can judge dart Kuo is io Library or html Library to choose to export different files :
export 'src/hw_none.dart' // Stub implementation
if (dart.library.io) 'src/hw_io.dart' // dart:io implementation
if (dart.library.html) 'src/hw_html.dart'; // dart:html implementationIt means , If in app Can be used in dart:io, Then export src/hw_io.dart.
If it can be used dart:html, Then export src/hw_html.dart, Otherwise, export src/hw_none.dart.
If it is conditional import , take export Change to import that will do .
Add other valid files
Because it's different library It has different functions , So you usually need to add some additional files to ensure library Effectiveness and integrity of .
In order to ensure library The effectiveness of the , You need to add test code , Test code is usually placed in test Directory .
If you are creating a command line tool , You need to put the corresponding tool in tools Directory .
And then there is README.md and CHANGELOG.md Wait for the documents .
library Documents
dart Documents can be used dartdoc This tool is used to generate .dart The document format in is in /// At the beginning , as follows :
/// The event handler responsible for updating the badge in the UI.
void updateBadge() {
...
}Publish to pub.dev
One of the best to share library The way is to send it to pub.dev On . The specific order is :pub publish.
summary
That's all dart Created in library The whole content of .
This article has been included in http://www.flydean.com/11-dart-create-package/ The most popular interpretation , The deepest dry goods , The most concise tutorial , There are so many tricks you don't know about waiting for you to discover !
边栏推荐
- CVPR2022 | 可精简域适应
- [applet] indicator of relative path and absolute path
- [planting grass by technology] 13 years' record of the prince of wool collecting on the cloud moving to Tencent cloud
- 实时计算框架:Flink集群搭建与运行机制
- How to improve program performance
- Cvpr2022 𞓜 thin domain adaptation
- Efficient integration of heterogeneous single cell transcriptome with scanorama
- Leetcode lecture on algorithm interview for large factories 2 Time space complexity
- 【ICCV Workshop 2021】基于密度图的小目标检测:Coarse-grained Density Map Guided Object Detection in Aerial Images
- Is it safe to open an account for shares of tongdaxin?
猜你喜欢

js输入输出语句,变量

ICML'22 | ProGCL: 重新思考图对比学习中的难样本挖掘
![[redis advanced ziplist] if someone asks you what is a compressed list? Please dump this article directly to him.](/img/3f/988ed31a3f1cdc92deed121ed8d4ec.png)
[redis advanced ziplist] if someone asks you what is a compressed list? Please dump this article directly to him.

An accident caused by a MySQL misoperation, and the "high availability" cannot withstand it!

飞桨产业级开源模型库:加速企业AI任务开发与应用

CVPR2022 | 可精简域适应

分别用SVM、贝叶斯分类、二叉树、CNN实现手写数字识别

Pad User Guide

Theoretical analysis of countermeasure training: adaptive step size fast countermeasure training

【机器学习】线性回归预测
随机推荐
ICML'22 | ProGCL: 重新思考图对比学习中的难样本挖掘
Thread safety and lock optimization
阿里巴巴面试题:多线程相关
How to view kubernetes API traffic by grabbing packets
Attack and defense world PyC trade
version `ZLIB_1.2.9‘ not found (required by /lib64/libpng16.so.16)
这不会又是一个Go的BUG吧?
一次 MySQL 误操作导致的事故,「高可用」都顶不住了!
【CVPR 2020】会议版本:A Physics-based Noise Formation Model for Extreme Low-light Raw Denoising
Application configuration management, basic principle analysis
Definition of logic
[applet] when compiling the preview applet, a -80063 error prompt appears
numpy. linalg. Lstsq (a, B, rcond=-1) parsing
Map data types in golang
Data management: business data cleaning and implementation scheme
How many of the 36 difficult points of activity do you know?, Android interview 2020
CVPR2022 | 可精简域适应
Vs2022 save formatting plug-in
paddle使用指南
对抗训练理论分析:自适应步长快速对抗训练